Objective c 旋转UIImageView的动画会旋转其他对象
到目前为止,我的代码是Objective c 旋转UIImageView的动画会旋转其他对象,objective-c,xcode,uiimageview,Objective C,Xcode,Uiimageview,到目前为止,我的代码是 rotateButton = [UIButton new]; rotateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [rotateButton setFrame:CGRectMake(334, 80, 100, 20)]; [rotateButton addTarget:self action:@selector(rotateImageView:Degrees:InTim
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旋转?更改
[rotateButton addTarget:self action:@selector(rotateImageView:Degrees:InTimePeriod:) forControlEvents:UIControlEventTouchUpInside];
到
您将@selector(rotateImageView:Degrees:InTimePeriod:)设置为按钮的操作。按钮将其自身作为@selector(rotateImageView:Degrees:InTimePeriod:)的第一个参数发送给默认值@selector(rotateImageView:Degrees:InTimePeriod:),事实上,当函数运行时,*imageView指向按钮,这就是为什么按钮会旋转,但图像视图会显示。按照aeubanks的说法执行。如果是这样,您的某个地方会出现错误。您可能不应该直接访问imageView,而是这样做:[self rotateImageView:self.imageView Degrees:90 InTimePeriod:1.5];
[rotateButton addTarget:self action:@selector(rotateImageView) forControlEvents:UIControlEventTouchUpInside];
[rotateButton addTarget:self action:@selector(rotateImageView:Degrees:InTimePeriod:) forControlEvents:UIControlEventTouchUpInside];