我的iOS自定义动画按钮在推送页面后不会缩小。有没有办法在太晚之前缩放我的按钮?因为当我返回原始页面时,我看到我的按钮仍然缩小了。
@interface CustomButton ()
@property (assign, nonatomic) BOOL isSmall;
@end
@implementation CustomButton
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder: decoder]) {
self.isSmall = NO;
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]){
self.isSmall = NO;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (!self.isSmall){
self.isSmall = YES;
[self scaleToSmall];
}
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.isSmall){
self.isSmall = NO;
[self scaleToBig];
}
[super touchesEnded:touches withEvent:event];
}
- (void)scaleToSmall
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
anim.duration = 0.100;
anim.repeatCount = 1;
anim.additive = YES;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)];
[self.layer addAnimation:anim forKey:nil];
}
- (void)scaleToBig
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
anim.duration = 0.100;
anim.repeatCount = 1;
anim.additive = YES;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.25, 1.25, 1.0)];
[self.layer addAnimation:anim forKey:nil];
}
答案 0 :(得分:0)
只要包含按钮的视图即将出现,您就可以尝试将其缩放到较大的尺寸。
在包含按钮的视图控制器的viewWillAppear:方法中执行此操作。
答案 1 :(得分:0)
我找到了解决方案。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (self.isSmall){
self.isSmall = NO;
[CATransaction begin];
[CATransaction setCompletionBlock:^{
[self.layer removeAllAnimations];
}];
[self scaleToBig];
[CATransaction commit];
}
[super touchesEnded:touches withEvent:event];
}