2013-04-29 67 views
0

刚刚有一个小问题。我试图让一个UIButton以编程方式出现,我必须进行动画制作。但是一旦动画完成,出于某种原因我无法点击按钮。我有一个CALayer“animationLayer”作为UIView的子层,在那个animationLayer中,我有三个CAShapeLayers“pathLayers”(我用它来动画三种不同的路径方式,如下图所示)。在这些路径层中,我添加了三个按钮作为它们各自的子层,这样我就可以获得一个动画,这些按钮随着路径的绘制沿着路径移动。一切都很好,直到我尝试添加一个按钮作为pathLayer的子层。现在当我点击按钮时,它应该转到一个方法登录控制台窗口“按钮已被按下”。我已经尝试将self.view.userInteractionenabled设置为YES,但仍然没有给我提供任何帮助。动画完成后,无法点击UIButton(以编程方式出现)

我环顾了几乎所有的地方,但无法弄清楚为什么我不能点击它。如果有人能帮助我,我会非常感激!

下面的代码的一些相关部分:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.animationLayer = [CALayer layer]; 
    self.animationLayer.frame = CGRectMake(20.0f, 64.0f, 
    CGRectGetWidth(self.view.layer.bounds) - 40.0f, CGRectGetHeight(self.view.layer.bounds) - 84.0f); 

    [self.view.layer addSublayer:self.animationLayer]; 

} 




- (id)createUIButtonwithXPosition:(CGFloat)x YPosition:(CGFloat)y Width:(CGFloat)width Height:(CGFloat)height 
{ 
    UIImage *lionImage = [UIImage imageNamed:@"noun_project_347_2.png"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button addTarget:self 
       action:@selector(logger:) 
    forControlEvents:UIControlEventTouchUpInside]; 
    button.frame = CGRectMake(x, y, width, height); 
    button.userInteractionEnabled = YES; 
// [button setBackgroundColor:[UIColor clearColor]]; 
    [button setBackgroundImage:lionImage forState:UIControlStateNormal]; 
    [self.view addSubview:button]; 

    return button; 
} 



- (void)setupDrawingLayer 
{ 
    if(self.pathLayer != nil){ 
     [self.pathLayer removeFromSuperlayer]; 
     [self.secondPathLayer removeFromSuperlayer]; 
     [self.thirdPathLayer removeFromSuperlayer]; 

     self.pathLayer = nil; 
     self.secondPathLayer = nil; 
     self.thirdPathLayer = nil; 

     ...//Other code not relevant 
} 

    CGRect pathRect = CGRectInset(self.animationLayer.bounds, 100.0f, 100.0f); 
    CGPoint bottomCenter = CGPointMake(CGRectGetMidX(pathRect), (self.lionBtnVerticalAlignment.constant + self.lionBtnHeight.constant + 25.0f)); 
    CGPoint center = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMidY(pathRect)); 
    CGPoint leftCenter = CGPointMake(CGRectGetMinX(pathRect), CGRectGetMidY(pathRect)); 
    CGPoint rightCenter = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMidY(pathRect)); 


    UIBezierPath *path = [UIBezierPath bezierPath]; 

    UIBezierPath *secondPath = [UIBezierPath bezierPath]; 

    UIBezierPath *thirdPath = [UIBezierPath bezierPath]; 


    [path moveToPoint:bottomCenter]; 
    [path addLineToPoint:center]; 

    [secondPath moveToPoint:bottomCenter]; 
    [secondPath addLineToPoint:leftCenter]; 

    [thirdPath moveToPoint:bottomCenter]; 
    [thirdPath addLineToPoint:rightCenter]; 

    CAShapeLayer *pathLayer = [CAShapeLayer layer]; 
    pathLayer.frame = self.animationLayer.bounds; 
    pathLayer.bounds = pathRect; 
    pathLayer.geometryFlipped = YES; 
    pathLayer.path = path.CGPath; 
    pathLayer.strokeColor = [UIColor blackColor].CGColor; 
    pathLayer.fillColor = nil; 
    pathLayer.lineWidth = 3.0f; 
    pathLayer.lineDashPattern = [NSArray arrayWithObjects: 
           [NSNumber numberWithInt:6], 
           [NSNumber numberWithInt:2], 
           nil]; 
    pathLayer.lineJoin = kCALineJoinBevel; 


    CAShapeLayer *secondPathLayer = [CAShapeLayer layer]; 
    secondPathLayer.frame = self.animationLayer.bounds; 
    secondPathLayer.bounds = pathRect; 
    secondPathLayer.geometryFlipped = YES; 
    secondPathLayer.path = secondPath.CGPath; 
    secondPathLayer.strokeColor = [UIColor redColor].CGColor; 
    secondPathLayer.fillColor = nil; 
    secondPathLayer.lineWidth = 3.0f; 
    secondPathLayer.lineDashPattern = [NSArray arrayWithObjects: 
           [NSNumber numberWithInt:6], 
           [NSNumber numberWithInt:2], 
           nil]; 
    secondPathLayer.lineJoin = kCALineJoinBevel; 

    CAShapeLayer *thirdPathLayer = [CAShapeLayer layer]; 
    thirdPathLayer.frame = self.animationLayer.bounds; 
    thirdPathLayer.bounds = pathRect; 
    thirdPathLayer.geometryFlipped = YES; 
    thirdPathLayer.path = thirdPath.CGPath; 
    thirdPathLayer.strokeColor = [UIColor greenColor].CGColor; 
    thirdPathLayer.fillColor = nil; 
    thirdPathLayer.lineWidth = 3.0f; 
    thirdPathLayer.lineDashPattern = [NSArray arrayWithObjects: 
             [NSNumber numberWithInt:6], 
             [NSNumber numberWithInt:2], 
             nil]; 
    thirdPathLayer.lineJoin = kCALineJoinBevel; 

    [self.animationLayer addSublayer:pathLayer]; 
    [self.animationLayer addSublayer:secondPathLayer]; 
    [self.animationLayer addSublayer:thirdPathLayer]; 

    self.pathLayer = pathLayer; 
    self.secondPathLayer = secondPathLayer; 
    self.thirdPathLayer = thirdPathLayer; 


    UIImage *lionImage = [UIImage imageNamed:@"noun_project_347_2.png"]; 

    btn1 = (UIButton *)[self createUIButtonwithXPosition:0.0f YPosition:0.0f Width:lionImage.size.width Height:lionImage.size.height]; 
    btn1.layer.anchorPoint = CGPointZero; 
    btn1.layer.frame = CGRectMake(0.0f, 0.0f, lionImage.size.width, lionImage.size.height); 

    [self.view bringSubviewToFront:btn1]; 
    self.view.userInteractionEnabled = YES; 

    ....//Other code not relevant 

} 



- (void)startAnimation 
{ 
    [self.pathLayer removeAllAnimations]; 
    [self.secondPathLayer removeAllAnimations]; 
    [self.thirdPathLayer removeAllAnimations]; 

    ....//Other code not relevant 

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
    pathAnimation.duration = 3.0; 
    pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
    pathAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 

    [self.pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; 
    [self.secondPathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; 
    [self.thirdPathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; 

    CAKeyframeAnimation *lionAnimation = [CAKeyframeAnimation  animationWithKeyPath:@"position"]; 
    lionAnimation.duration=3.0; 
    lionAnimation.path = self.pathLayer.path; 
    lionAnimation.calculationMode = kCAAnimationPaced; 
    lionAnimation.delegate = self; 

    [btn1.layer addAnimation:lionAnimation forKey:@"position"]; 

    btn1.layer.position = CGPointMake(365.0, 460.0); 
} 
+0

你确定在'365.0,460.0'(不管按钮是否出现在那里)点击不起作用吗? – 2013-04-29 05:33:21

+0

是的,先生,我很确定。 – Enigma365 2013-04-30 00:32:24

回答

-1

请试试这个:

[button setBackgroundImage:lionImage forState:UIControlEventTouchUpInside]; 
+0

嘿!我试图做到这一点,但它只是使图像消失,它没有做任何事情关于点击问题。还有其他建议吗? – Enigma365 2013-04-29 01:59:15

+0

上面的代码在你按右键时工作吗?与日志消息“按钮已被按下” – Sovannarith 2013-04-29 02:03:20

+0

不,问题是我不能单击按钮。所以,它甚至不会去按钮的操作方法(按下时)。我认为,因为我在CALayer内部有一个UIButton,所以按钮位于图层的后面,因此无法按下它(至少我认为这是)。因为UIView本身可以被看作是一个CALayer,所以我想要做的事情不应该很难..大声笑,但由于某种原因,我不能让按钮点击。 – Enigma365 2013-04-29 02:18:29

1

在我看来,你可能会遇到的一个典型问题,其中UIView为子子另一个UIView,但超出其界限。要检查是否属于这种情况,请将父视图的属性设置为setClipsToBounds:YES,您很可能会看到按钮消失。检查这个帖子的其他细节:My custom UIView dos not receive touches

如果是这种情况,你有几个选项。

  1. 将按钮放在它在父视图范围内的地方!
  2. 继承父视图的子类,并将以下代码添加到以使其子视图可能响应触摸,即使它们位于父视图的范围之外。

    - (UIView的*)则hitTest:(CGPoint)点withEvent:方法(的UIEvent *)事件{

    的UIView *结果= [超级则hitTest:点withEvent:方法事件]; if(result) return result;对于(UIView * sub in [self.subviews reverseObjectEnumerator]){ CGPoint pt = [self convertPoint:point toView:sub]; result = [sub hitTest:pt withEvent:event]; if(result) return result; } return nil; }

+0

嘿哈维尔!我尝试了整个setClipsToBounds:YES过程,但显然情况并非如此。该按钮的图像仍然存在。我现在想要做的是使用图像来完成整个动画,一旦动画完成,我将删除相应的图层,并在图层结束动画的同一位置用UIButton替换它们。这是一个好方法还是你有其他想法? – Enigma365 2013-04-30 00:30:15