0

我需要旋转我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)

重要的是您始终可以使用计时器来移动对象,因此该部分是不可更改的。

4

1 回答 1

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用于旋转。

于 2012-11-29T10:17:06.743 回答