我需要轮换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
。
我还尝试使用CGAffineTransform旋转UIImageView
,但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)
重要的是你总是可以使用计时器来移动对象,这样那部分是不可更改的。
答案 0 :(得分:0)
我找到了解决方案!首先,我们排除提供UIImage
调整大小的解决方案。所以,我们旋转UIImageView
,但是当使用CGAffineTransform时,框架不能被修改!因此,要使用计时器移动UIImageView
,我们必须修改UIImageView
的中心。这是删除:
CGRect viewLocation = [[[self layer] presentationLayer] frame];
self.frame = CGRectMake(viewLocation.origin.x, viewLocation.origin.y + 0.2, viewLocation.size.width, viewLocation.size.height);
这是补充:
self.center = CGPointMake(self.frame.origin.x + (self.frame.size.width / 2), self.frame.origin.y + 0.2 + (self.frame.size.height / 2));
然后,我们可以使用rotateWithDegrees:
方法来轮播UIImageView
:
- (void)rotateWithDegrees:(CGFloat)degrees {
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
self.transform = transform;
_degrees+=0.5;
if (_degrees == 360) {
_degrees = 1;
}
}
所以,我们可以添加一行:
[self rotateWithDegrees:_degrees];
下面:
self.center = CGPointMake(self.frame.origin.x + (self.frame.size.width / 2), self.frame.origin.y + 0.2 + (self.frame.size.height / 2));
UIImageView
由于其中心的变化而移动,而不是由于其框架的更改而允许使用CGAffineTransform
进行旋转。