我必须为UIButtons数组执行缩小和扩展动画。对于单个UIButton,我这样做......
UIButton *button = [self.destinationButtonsArray objectAtIndex:0];
[UIView beginAnimations:@"shrink" context:(__bridge void *)(button)];
[UIView animateWithDuration:0.7f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction animations:^{
[UIView setAnimationRepeatCount:3];
CGAffineTransform t = CGAffineTransformMakeScale(1.2f, 1.2f);
button.transform = t;
} completion:^(BOOL finished) {
button.transform = CGAffineTransformMakeScale(1.0f, 1.0f);}];
如何为UI按钮阵列实现相同的效果。
答案 0 :(得分:1)
您可以使用类别。声明UIButton
类别并添加方法以执行动画。
<强>的UIButton + Transform.h 强>
@interface UIButton (Transform)
- (void) applyAnimation;
@end
<强>的UIButton + Transform.m 强>
@implementation UIButton (Transform)
- (void) applyAnimation {
[UIView beginAnimations:@"shrink" context:self];
[UIView animateWithDuration:0.7f
delay:0
options:UIViewAnimationOptionAutoreverse
| UIViewAnimationCurveEaseInOut
| UIViewAnimationOptionRepeat
| UIViewAnimationOptionAllowUserInteraction
animations:^{
[UIView setAnimationRepeatCount:3];
CGAffineTransform t = CGAffineTransformMakeScale(1.2f, 1.2f);
self.transform = t;
}
completion:^(BOOL finished) {
self.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
}
];
}
@end
按如下方式调用数组上的方法
[self.destinationButtonsArray makeObjectsPerformSelector:@selector(applyAnimation)];
这将在数组中的所有按钮上调用animation方法。
希望有所帮助!
答案 1 :(得分:-1)
在动画块中使用for
,例如:
[UIView beginAnimations:@"shrink" context:(__bridge void *)(button)];
[UIView animateWithDuration:0.7f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction animations:^{
[UIView setAnimationRepeatCount:3];
CGAffineTransform t = CGAffineTransformMakeScale(1.2f, 1.2f);
for (UIButton button in self.destinationButtonsArray) {
button.transform = t;
}
} completion:^(BOOL finished) {
for (UIButton button in self.destinationButtonsArray) {
button.transform = CGAffineTransformMakeScale(1.0f, 1.0f);}];
}
}];