繁体   English   中英

为什么我的图像不能在Xcode中旋转?

[英]Why Won't My Image Rotate in Xcode?

我正在尝试做一个简单的动画,当单击一个按钮时,它将连续不断地旋转图像,但是由于某些原因,当我单击该按钮时,它会使应用程序崩溃。

这是我的代码。

.h文件:

@interface MainView : UIView {


}

IBOutlet UIImageView *sunray;

- (IBAction)pushrotate:(id)sender;



@end

.m文件:

@implementation MainView


- (IBAction)pushrotate:(id)sender {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0];
    sunray.transform = CGAffineTransformMakeRotation(6.2831855);
    [UIView commitAnimations];

}

-(void) dealloc {

    [sunray dealloc];
    [sunray release];
    [super dealloc];

}
@end

知道如何使它起作用吗? 此外,使动画在应用程序加载后立即开始的最佳方法是什么?

这是我旋转图像的解决方案

    -(void)rotate:(int)degree {
       float rad = M_PI * (float)degree / 180.0;

       [UIView beginAnimations:nil context:nil];
       [UIView setAnimationDuration:0];
       [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       aktivImageView.transform = CGAffineTransformMakeRotation(rad);

       [UIView commitAnimations];
    }

我认为您也可以通过按钮使用此代码片段

在您的IBAction中,您可以编码[自我旋转:90]; 以90°进行评级,或者您可以使用[自我旋转:0]; 不旋转图像;-)

如果将视图旋转角度设置为2 * PI的倍数(在您的情况下看起来如此),则视图会识别出变换实际上没有效果,并且视图也不会旋转。

要使用动画将角度旋转到大于2 * PI的角度,您需要使用CoreAnimation。 因此,您的代码应如下所示:

//link with QuartzCore.framework
#import <QuartzCore/QuartzCore.h>    
...
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 1.0f;
[sanray.layer addAnimation:animation forKey:@"MyAnimation"];

您可能将操作或插座连接错误(如果您发布了更多控制台输出,我们将确定)。 查看Interface Builder在您的操作/出口上是否有任何错误标记。

另请参阅我关于自己调用dealloc的评论-您不应该这样做,除了您自己的dealloc实现中的[super dealloc]外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM