【发布时间】:2012-07-21 17:53:45
【问题描述】:
嗨,
我正在尝试在两个不同的图像之间为 UIButton 的图像设置动画。
假设我有两个正方形图像,红色和黑色。
当我触摸我的按钮时,我希望 uibutton 的图像以流畅的动画从两个图像中循环播放。
当用户按下按钮时循环动画必须开始,当他在里面触摸或拖动退出时取消。
这是我现在要做的:
-(void)startAnimation:(UIButton *)obj {
[UIView animateWithDuration:1 delay:0 options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{
[obj setAlpha:0];
[obj setImage:[UIImage imageNamed:@"animated_on"] forState:UIControlStateNormal];
[obj setImage:[UIImage imageNamed:@"animated_on"] forState:UIControlStateHighlighted];
[obj setImage:[UIImage imageNamed:@"animated_on"] forState:UIControlStateDisabled];
[obj setImage:[UIImage imageNamed:@"animated_on"] forState:UIControlStateSelected];
} completion:^(BOOL finished) {
[obj setAlpha:1];
[obj setImage:nil forState:UIControlStateNormal];
[obj setImage:nil forState:UIControlStateHighlighted];
[obj setImage:nil forState:UIControlStateDisabled];
[obj setImage:nil forState:UIControlStateSelected];
}];
}
-(void)stopAnimation:(UIButton *)obj {
[obj.layer removeAllAnimations];
}
- (void)touchDownAnimation:(id)obj {
[self startAnimation:obj];
}
- (void)dragEnterAnimation:(id)obj {
[self startAnimation:obj];
}
- (void)dragExitAnimation:(id)obj {
[self stopAnimation:obj];
}
- (void)touchUpInsideAnimation:(id)obj {
[self stopAnimation:obj];
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(110, 180, 100, 100);
UIImageView *animatedButtonBackground = [[UIImageView alloc] initWithFrame:frame];
[animatedButtonBackground setImage:[UIImage imageNamed:@"animated_off"]];
[self.view addSubview:animatedButtonBackground];
UIButton *animatedButton = [UIButton buttonWithType:UIButtonTypeCustom];
[animatedButton setFrame:frame];
[animatedButton addTarget:self action:@selector(touchDownAnimation:) forControlEvents:UIControlEventTouchDown];
[animatedButton addTarget:self action:@selector(dragEnterAnimation:) forControlEvents:UIControlEventTouchDragEnter];
[animatedButton addTarget:self action:@selector(dragExitAnimation:) forControlEvents:UIControlEventTouchDragExit];
[animatedButton addTarget:self action:@selector(touchUpInsideAnimation:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:animatedButton];
}
它有点工作,但在过程中涉及 UIImageView 看起来像一个坏把戏来做我想做的事......
我正在寻找更清洁的解决方案。
【问题讨论】:
标签: objective-c animation uibutton