2013-10-14 24 views
1

我试图制作一个自定义视图,其中一个元素(UILabel)具有动画,当点击自定义视图的按钮时,标签应该展开然后收缩。动画的第一部分正确发生,但不是第二部分。我使用这种方法。使用动画创建自定义视图

[UIView animateWithDuration:1.3 animations:^{ 
    CGRect frame = CGRectMake(50, 15, 140, 20); 
    [labelFav setFrame:frame]; 
} completion:^(BOOL finished) { 
    //[labelFav setHidden:YES]; 
    [UIView animateWithDuration:1.3 animations:^{ 
     CGRect frame = CGRectMake(50, 15, 0, 20); 
     [labelFav setFrame:frame]; 
    }]; 

}]; 

我相信这是如何实现Refresh和LayoutSubviews方法的问题。 我做了以下这个教程,ray wenderlinch

问题是当第一动画被称为后,LayoutSubviews被调用,因此,所有的项目都设置到初始位置,并且当第一动画结束后第二个开始但项目设置为原始状态,所以我相信动画不会发生,因为动画之间有变化。

我想知道如何正确实施,你能帮我一把吗?

谢谢。 PS:这是关于同一个问题的第二个问题,但是角度不同。 my previous question

[更新]

这是我LayoutSubviews代码:

- (void)layoutSubviews { 
[super layoutSubviews]; 

if(_idEvent == 0) return; 

_buttonFav = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; 
[_buttonFav setImage:[UIImage imageNamed:@"fav.png"] forState:UIControlStateNormal]; 
[_buttonFav addTarget:self action:@selector(pressedFavButton:) forControlEvents:UIControlEventTouchDown]; 

_labelButton = [[UILabel alloc]initWithFrame:CGRectMake(0, 45, 0, 20)]; 
_labelButton.text = @"LABEL"; 

[self addSubview:_labelButton]; 
[self addSubview:_buttonFav]; 

}

这是第二次动画之前正确调用。我的刷新方法是:

- (void)refresh { 
if(selected){ 

    [_buttonFav setImage:[UIImage imageNamed:@"fav_sel.png"] forState:UIControlStateNormal]; 
} 
else{ 
    [_buttonFav setImage:[UIImage imageNamed:@"fav.png"] forState:UIControlStateNormal]; 
} 

}

如果我只是设置取决于让我知道,如果按钮被窃听的属性按钮图像。

+0

只需确认:此项目中不使用自动布局? –

+0

我这样做,如何用自动布局制作动画呢?使用新的XCode,你没有一个选项来禁用它。 – subharb

回答

1

我,如何使自动布局,然后动画?使用新的 XCode,你没有一个选项来禁用它。

将宽度约束添加到标签。然后创建IBOutlet这个约束在实现文件(见the section 3. here,如果你不知道如何做到这一点),让我们这样说:

[UIView animateWithDuration:1.3 animations:^{ 
    self.buttonWidthConstraint.constant = 140; 
    [self.view layoutIfNeeded]; 
} completion:^(BOOL finished) { 

    [UIView animateWithDuration:1.3 animations:^{ 
     self.buttonWidthConstraint.constant = 0; 
     [self.view layoutIfNeeded]; 
    }]; 

}]; 

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonWidthConstraint; 

然后你就可以用这种方式制作动画

+0

我不相信这是一个自动布局问题,因为我试图将动画更改为“淡入”“淡出”动画和事件,尽管它没有动画效果,我认为这是因为LayoutSubview在第二个布局被调用。 我会告诉你LayoutSubviews的代码 – subharb

+0

这是一个自动布局问题。使用自动布局时,不应手动设置框架。只是谷歌:自动布局更改框架。 [这里是我的代码如何工作的视频](https://www.dropbox.com/s/dppsmr6vu4nju5g/auto_layout_resize.mov)(按钮标题隐藏,但这是另一个问题:))。 –

+0

我会尝试,但即时通讯不是由接口生成器,但通过代码设置我的UIView,我该如何设置该代码的NSLayoutConstraint? 感谢您的帮助 – subharb

1

使用的setTransform和CGAffineTransform

尝试了这一点

[UIView animateWithDuration:1.3 animations:^{ 

    [labelFav setTransform:CGAffineTransformMakeScale(0.5,0.5)]; 
} completion:^(BOOL finished) { 

    [UIView animateWithDuration:1.3 animations:^{ 

     [labelFav setTransform:CGAffineTransformMakeScale(1.0,1.0)]; 
    }]; 

}]; 
0

尝试封装的UILabel到一个UIView和动画这一观点。

UIView *auxView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)]; 
[auxView addSubview:labelFav]; 
[UIView animateWithDuration:1.3 animations:^{ 
    CGRect frame = CGRectMake(50, 15, 140, 20); 
    [auxView setFrame:frame]; 
} completion:^(BOOL finished) { 
    //[labelFav setHidden:YES]; 
     [UIView animateWithDuration:1.3 animations:^{ 
      CGRect frame = CGRectMake(50, 15, 10, 20); 
      [auxView setFrame:frame]; 
     }]; 
}];