在具有填充模式的 CAKeyFrameAnimation 之后无法与 UIButton 交互
Unable to interact with UIButton after CAKeyFrameAnimation with fill modes
我在 IB 中有一个名为 ButtonHolderView 的 UIView
。我在此视图中添加了一个 UIButton
并对其进行了动画处理。我设置了委托,以便在按下按钮时通知用户。但是,动画完成后,我无法点击按钮。我可以清楚地看到按钮在设备和视图调试器的视图上。
截图如下:
在设备上:
动画:
按钮向左走,再向右走,再向左走a,再向右走,停!!!
代码:
ViewController.m
属性 声明:
@property (nonatomic,strong) IBOutlet ButtonHolderView *butTopHolder;
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_butTopHolder.buttonHolderViewDelegate=self;
[_butTopHolder addButtonViewAnimationWithCompletion:^(BOOL finished) {
NSLog(@"completed");
}];
}
并且在ButtonHolderView.m中:
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setupHierarchy];
}
return self;
}
- (void)setupHierarchy
{
UIView *scaling = [UIView new];
scaling.clipsToBounds=NO; //tried to set NO
scaling.bounds = CGRectMake(0, 0, 320, 568);
scaling.center = CGPointMake(320.0, 568.0);
scaling.layer.masksToBounds = NO; //tried to set NO
[self addSubview:scaling];
UIButton *aboutme = [UIButton buttonWithType:UIButtonTypeCustom];
aboutme.userInteractionEnabled=YES;
aboutme.bounds = CGRectMake(0, 0, 50.0, 50.0);
UIImage *imgAboutme = [UIImage imageWithContentsOfFile:[bundle pathForResource:@"aboutme.png" ofType:nil]];
[aboutme setBackgroundImage:imgAboutme forState:UIControlStateNormal];
aboutme.contentMode = UIViewContentModeCenter;
aboutme.layer.position = CGPointMake(80, 80);
aboutme.alpha = 0.00;
[aboutme addTarget:self action:@selector(actionAboutmePressed:) forControlEvents:UIControlEventTouchUpInside];
[scaling addSubview:aboutme];
}
- (void)addButtonViewAnimationWithCompletion:(void (^)(BOOL finished))completionBlock
{
[self addButtonViewAnimationWithBeginTime:0 andFillMode:kCAFillModeBoth andRemoveOnCompletion:NO completion:completionBlock];
}
- (void)addButtonViewAnimationWithBeginTime:(CFTimeInterval)beginTime andFillMode:(NSString *)fillMode andRemoveOnCompletion:(BOOL)removedOnCompletion completion:(void (^)(BOOL finished))completionBlock
{
CAMediaTimingFunction *linearTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CAKeyframeAnimation *aboutmeTranslationXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
aboutmeTranslationXAnimation.duration = 1.150;
aboutmeTranslationXAnimation.values = @[@(0.000), @(-130.000), @(100.000), @(-20.000), @(70.000), @(0.000)];
aboutmeTranslationXAnimation.keyTimes = @[@(0.000), @(0.261), @(0.435), @(0.609), @(0.783), @(1.000)];
aboutmeTranslationXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming, linearTiming, linearTiming];
aboutmeTranslationXAnimation.beginTime = beginTime;
aboutmeTranslationXAnimation.fillMode = fillMode;
aboutmeTranslationXAnimation.removedOnCompletion = removedOnCompletion;
}
当使用FillMode:kCAFillModeBoth
时,动画后,我能看到设备上的按钮,但不能触摸它
当使用FillMode:kCAFillModeRemoved/Forward/Backward
时,动画后,我在设备上看不到按钮,但在视图调试器上可以看到!!!
我尝试将 masksToBounds 设置为 NO,但没有成功。
谁能建议我解决这个问题的方法?
真傻,我不知道如果按钮alpha为0,它就不能接收任何触摸!!!!
最初我将按钮的 alpha 设置为 0 并增加了它的 alpha,一旦动画完成,按钮将它的 alpha 恢复为 0,因此它无法接收任何触摸!!!
在我将 alpha 设置为 1 之后,我可以触摸按钮了!!!
我在 IB 中有一个名为 ButtonHolderView 的 UIView
。我在此视图中添加了一个 UIButton
并对其进行了动画处理。我设置了委托,以便在按下按钮时通知用户。但是,动画完成后,我无法点击按钮。我可以清楚地看到按钮在设备和视图调试器的视图上。
截图如下:
在设备上:
动画:
按钮向左走,再向右走,再向左走a,再向右走,停!!!
代码:
ViewController.m
属性 声明:
@property (nonatomic,strong) IBOutlet ButtonHolderView *butTopHolder;
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_butTopHolder.buttonHolderViewDelegate=self;
[_butTopHolder addButtonViewAnimationWithCompletion:^(BOOL finished) {
NSLog(@"completed");
}];
}
并且在ButtonHolderView.m中:
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setupHierarchy];
}
return self;
}
- (void)setupHierarchy
{
UIView *scaling = [UIView new];
scaling.clipsToBounds=NO; //tried to set NO
scaling.bounds = CGRectMake(0, 0, 320, 568);
scaling.center = CGPointMake(320.0, 568.0);
scaling.layer.masksToBounds = NO; //tried to set NO
[self addSubview:scaling];
UIButton *aboutme = [UIButton buttonWithType:UIButtonTypeCustom];
aboutme.userInteractionEnabled=YES;
aboutme.bounds = CGRectMake(0, 0, 50.0, 50.0);
UIImage *imgAboutme = [UIImage imageWithContentsOfFile:[bundle pathForResource:@"aboutme.png" ofType:nil]];
[aboutme setBackgroundImage:imgAboutme forState:UIControlStateNormal];
aboutme.contentMode = UIViewContentModeCenter;
aboutme.layer.position = CGPointMake(80, 80);
aboutme.alpha = 0.00;
[aboutme addTarget:self action:@selector(actionAboutmePressed:) forControlEvents:UIControlEventTouchUpInside];
[scaling addSubview:aboutme];
}
- (void)addButtonViewAnimationWithCompletion:(void (^)(BOOL finished))completionBlock
{
[self addButtonViewAnimationWithBeginTime:0 andFillMode:kCAFillModeBoth andRemoveOnCompletion:NO completion:completionBlock];
}
- (void)addButtonViewAnimationWithBeginTime:(CFTimeInterval)beginTime andFillMode:(NSString *)fillMode andRemoveOnCompletion:(BOOL)removedOnCompletion completion:(void (^)(BOOL finished))completionBlock
{
CAMediaTimingFunction *linearTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
CAKeyframeAnimation *aboutmeTranslationXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
aboutmeTranslationXAnimation.duration = 1.150;
aboutmeTranslationXAnimation.values = @[@(0.000), @(-130.000), @(100.000), @(-20.000), @(70.000), @(0.000)];
aboutmeTranslationXAnimation.keyTimes = @[@(0.000), @(0.261), @(0.435), @(0.609), @(0.783), @(1.000)];
aboutmeTranslationXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming, linearTiming, linearTiming];
aboutmeTranslationXAnimation.beginTime = beginTime;
aboutmeTranslationXAnimation.fillMode = fillMode;
aboutmeTranslationXAnimation.removedOnCompletion = removedOnCompletion;
}
当使用FillMode:kCAFillModeBoth
时,动画后,我能看到设备上的按钮,但不能触摸它
当使用FillMode:kCAFillModeRemoved/Forward/Backward
时,动画后,我在设备上看不到按钮,但在视图调试器上可以看到!!!
我尝试将 masksToBounds 设置为 NO,但没有成功。
谁能建议我解决这个问题的方法?
真傻,我不知道如果按钮alpha为0,它就不能接收任何触摸!!!!
最初我将按钮的 alpha 设置为 0 并增加了它的 alpha,一旦动画完成,按钮将它的 alpha 恢复为 0,因此它无法接收任何触摸!!!
在我将 alpha 设置为 1 之后,我可以触摸按钮了!!!