如何继续旋转图像

时间:2010-11-22 05:18:34

标签: iphone

有一个按钮,当我点击按钮时,我想将图像旋转180,但只有当我第一次点击按钮时,它才有效,如何继续旋转图像,提前感谢? 以下是我的源代码:

- (IBAction)touchButton_Refresh {

photo *photos = [photoArray objectAtIndexhotoIndex];
NSData *xmlData = [NSData dataWithContentsOfFilehotos.localPath];
UIImage *image;
if ([xmlData isKindOfClass:[NSData class]]) {
image = [[UIImage imageWithData:xmlData] retain];
}


SlideItem *slideItemRotate;


if (toOrientation == 1 || toOrientation == 2) {
slideItemRotate = [[SlideItem alloc] initWithFrame:CGRectMake(768*photoIndex, 0, 768, 980)];
}
else if (toOrientation == 3 || toOrientation == 4) {
slideItemRotate = [[SlideItem alloc] initWithFrame:CGRectMake(1024*photoIndex, 0, 1024, 700)];
}


slideItemRotate.photoImageView.image = image;

CGAffineTransform rotation = CGAffineTransformMakeRotation(3.14159265);
[slideItemRotate.photoImageView setTransform:rotation];

[[[slideScrollView subviews] objectAtIndex:0] removeFromSuperview];

[slideScrollView addSubview:slideItemRotate];


[slideItemRotate release];


} 

1 个答案:

答案 0 :(得分:1)

你必须连接变换,否则你只是继续应用相同的旋转(实际上不再旋转它)。所以,替换这些行:

CGAffineTransform rotation = CGAffineTransformMakeRotation(3.14159265);
[slideItemRotate.photoImageView setTransform:rotation];

用这个:

CGAffineTransform rotation = CGAffineTransformConcat([[slideItemRotate photoImageView] transform], CGAffineTransformMakeRotation(3.14159265));
[slideItemRotate.photoImageView setTransform:rotation];

这实际上会连接旋转并保持图像旋转。如果你总是在180度附近去做另一种方法就是测试身份变换。如果视图的变换是标识变换,则应用旋转。如果不是,请将变换重置为标识变换(有效地反转过程)。