CAKeyFrameAnimation与填充模式

时间:2015-11-18 20:55:54

标签: ios objective-c uibutton cakeyframeanimation

我在IB中有一个名为ButtonHolderView的UIView。我在此视图中添加了UIButton并对其进行了动画处理。我设置了委托,这样当按下按钮时,我会通知用户。但是,动画完成后,我无法点击按钮。我可以清楚地看到该按钮位于设备和视图debuggger的视图中。

以下是截图:

enter image description here

并在设备上:

enter image description here

动画:

按钮向左移动,然后向右移动,然后向左移动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,但它没有用。

有人可以建议我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

太傻了,我不知道如果按钮alpha为0,它就无法接受任何触摸!!!!

最初我将按钮alpha设置为0并增加其alpha值,一旦动画完成,按钮将恢复为0,因此无法接收任何触摸!

将alpha设为1后,我可以触摸按钮!