【问题标题】:UIButton rotate with CABasicAnimation does not move initial touch coordinates使用 CABasicAnimation 旋转的 UIButton 不会移动初始触摸坐标
【发布时间】:2014-08-24 10:08:55
【问题描述】:

我有一个 UIView,我在上面添加了一个 UIButton。我将 UIView 旋转 180 度。旋转 UIView 后,它里面的所有东西看起来都旋转了,包括按钮。但是当我点击按钮时。它仅从其初始位置接收触摸事件。不是从它出现在屏幕上的位置。这是我的代码 -

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
fullRotation.fromValue = [NSNumber numberWithFloat:0];
fullRotation.toValue = [NSNumber numberWithFloat:((180*M_PI)/180)];
fullRotation.speed = 1.0;
fullRotation.autoreverses = NO;
fullRotation.removedOnCompletion = NO;
fullRotation.fillMode = kCAFillModeForwards;
[_subView.layer addAnimation:fullRotation forKey:@"360"];

【问题讨论】:

    标签: ios rotation


    【解决方案1】:

    您无法从 Interface Builder 中执行此操作。您必须从代码中旋转它。像这样,

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
        [button addTarget:self action:@selector(selectedButton) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundColor:[UIColor purpleColor]];
    
        CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        fullRotation.fromValue = [NSNumber numberWithFloat:0];
        fullRotation.toValue = [NSNumber numberWithFloat:((180*M_PI)/180)];
        fullRotation.speed = 1.0;
        fullRotation.autoreverses = NO;
        fullRotation.removedOnCompletion = NO;
        fullRotation.fillMode = kCAFillModeForwards;
        [button.layer addAnimation:fullRotation forKey:@"360"];
    
        [self.view addSubview:button];
    

    现在selectedButton 将接收每个旋转的 UIButton 触摸。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多