【问题标题】:Rotating animation for UIImageView rotates something elseUIImageView 的旋转动画会旋转其他东西
【发布时间】:2013-03-07 01:47:34
【问题描述】:

到目前为止我的代码是

rotateButton = [UIButton new];
    rotateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [rotateButton setFrame:CGRectMake(334, 80, 100, 20)];
    [rotateButton addTarget:self action:@selector(rotateImageView:Degrees:InTimePeriod:) forControlEvents:UIControlEventTouchUpInside];
    [rotateButton setTitle:@"Rotate" forState:UIControlStateNormal];
    [self.view addSubview:rotateButton];

-(void)rotateImageView
{
    [self rotateImageView:imageView Degrees:90 InTimePeriod:1.5];
}

-(void)rotateImageView:(UIImageView*)imageView Degrees:(NSInteger)degrees InTimePeriod:(float)time
{
    CABasicAnimation *rotate;
    rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    rotate.fromValue = [NSNumber numberWithFloat:0];
    rotate.toValue = [NSNumber numberWithFloat:DEGREES_TO_RADIANS(degrees)];
    rotate.duration = time;
    rotate.repeatCount = 1;
    [imageView.layer addAnimation:rotate forKey:@"Rotate"];
}

但是,我用来开始旋转的按钮是我按下按钮后旋转的对象,而不是我希望它旋转的 imageView。如何让 imageView 旋转?

【问题讨论】:

    标签: objective-c xcode uiimageview


    【解决方案1】:
     [rotateButton addTarget:self action:@selector(rotateImageView:Degrees:InTimePeriod:) forControlEvents:UIControlEventTouchUpInside];
    

    您将@selector(rotateImageView:Degrees:InTimePeriod:) 设置为按钮的操作。按钮将自己作为第一个参数发送给@selector(rotateImageView:Degrees:InTimePeriod:) 作为默认值,事实上,当函数正在运行,*imageView 指向按钮,这就是为什么按钮旋转但 imageView.Do as aeubanks 说的。

    【讨论】:

      【解决方案2】:

      改变

          [rotateButton addTarget:self action:@selector(rotateImageView:Degrees:InTimePeriod:) forControlEvents:UIControlEventTouchUpInside];
      

          [rotateButton addTarget:self action:@selector(rotateImageView) forControlEvents:UIControlEventTouchUpInside];
      

      【讨论】:

      • 在这种情况下,您在某处有错误。您可能不应该直接访问 imageView,而是这样做: [self rotateImageView:self.imageView Degrees:90 InTimePeriod:1.5];
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      相关资源
      最近更新 更多