0

ちょっとした質問がありました。UIButton をプログラムで表示させようとしていますが、アニメーション化する必要があります。しかし、アニメーションが完了すると、何らかの理由でボタンをクリックできなくなります。

UIView のサブレイヤーとして CALayer "animationLayer" があり、その animationLayer 内に 3 つの CAShapeLayers "pathLayers" があります (下の図でわかるように、3 つの異なるパスウェイをアニメーション化するために使用します)。これらの pathLayers 内に、それぞれのサブレイヤーとして 3 つのボタンを追加して、パスが描画されるときにボタンがパスに沿って移動するアニメーションを作成できるようにします。pathLayer のサブレイヤーとしてボタンを追加しようとするまで、すべてがうまく機能していました。ボタンをクリックすると、「ボタンが押されました」というコンソールウィンドウにログオンするメソッドに移動するはずです。self.view.userInteractionenabled を YES に設定しようとしましたが、それでも何も得られません。

クリックできないのはなぜですか?

コードの関連部分を次に示します。

- (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);
}
4

2 に答える 2