我需要旋转我UIImageView
的 或UIImage
中的UIImageView
,同时用 移动它的框架NSTimer
。这是NSTimer
运动:
timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(animate) userInfo:nil repeats:YES];
这是动画方法(它的一部分,最重要的):
CGRect viewLocation = [[[self layer] presentationLayer] frame];
self.frame = CGRectMake(viewLocation.origin.x, viewLocation.origin.y + 0.2, viewLocation.size.width, viewLocation.size.height);
这是我试图用来旋转 UIImage 的方法:
- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees
{
// calculate the size of the rotated view's containing box for our drawing space
UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.size.width, self.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;
[rotatedViewBox release];
// Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
// Move the origin to the middle of the image so we will rotate and scale around the center.
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
// Rotate the image context
CGContextRotateCTM(bitmap, DegreesToRadians(degrees));
// Now, draw the rotated/scaled image into the context
CGContextScaleCTM(bitmap, 1.0, -1.0);
CGContextDrawImage(bitmap, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), [self CGImage]);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
此代码位于 的子类中UIImageView
,因此引用了 self UIImageView
。
我也尝试UIImageView
用 CGAffineTransform 旋转,但UIImageView
它被拉伸并以一种奇怪的方式旋转。这是方法:
- (void)rotateWithDegrees:(CGFloat)degrees {
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
self.transform = transform;
}
这是宏:
#define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)
重要的是您始终可以使用计时器来移动对象,因此该部分是不可更改的。