1

我将 UIImageView 子类化为允许我旋转图像,或者使用用户的手指拖动图像,或者使用滑块来设置图像旋转。

这一切在最初使用时都可以正常工作,但是如果用户旋转图像然后重新定位设备,那么图像大小会被严重搞砸 - 如果用户顺时针旋转图像,那么图像会变得很大,逆时针图像变小了。

如果我将 UIImageView 的 autoresizingMask 设置为 none,那么问题就会消失,但是如果我这样做,那么我将需要在设备旋转时手动移动/重新调整视图的大小,我不希望这样做。

以下代码完成了大部分工作:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    self.spinInProgress = NO;
    if (!self.spinnable || [[event allTouches] count]>1) {
        [super touchesBegan:touches withEvent:event];
        return;
    }
    UITouch *touch = (UITouch*)[touches anyObject];

    //check that touch is within radius of the circle
    float radius = MIN(self.frame.size.height, self.frame.size.width);
    radius = radius / 2;
    CGPoint centre = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);

    CGPoint posInView = [touch locationInView:self];

    float x = posInView.x;
    float y = posInView.y;
    float centre_x = centre.x;
    float centre_y = centre.y;


    if ((powf(x - centre_x, 2) + powf(y - centre_y, 2)) > powf(radius, 2))
        return; //not within the circle..

    //so now doing the work
    self.spinInProgress = YES;

    //stop the spinning if it is doing so
    if (spinTimer!=nil && [spinTimer isValid]){
        [spinTimer invalidate];
        spinTimer = nil;
    }

    //store the current touch as the previous touch position for now for future calculation when touch moves
    CGPoint previousPosition = [touch locationInView:self.superview];

    CGPoint lowerLeft = self.frame.origin;
    CGSize size = self.frame.size;

    //find the centre of the image (really should cache this!)

    centreX = (size.width/2) + lowerLeft.x;
    centreY = (size.height/2) + lowerLeft.y;

    startX = previousPosition.x;
    startY = previousPosition.y;

    //work out the angle of the current touch
    float startdx = previousPosition.x - centreX;
    float startdy = previousPosition.y - centreY;
    startAngle = atan2(startdx, startdy) *180/M_PI;

    startAngle += self.currentAngle;

    [super touchesBegan:touches withEvent:event];


}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    if (!self.spinInProgress) {
        [super touchesMoved:touches withEvent:event];
        return;
    }

    UITouch *touch = (UITouch*)[touches anyObject];
    //work out the current position of the touch
    CGPoint currentPosition = [touch locationInView:self.superview];

    //calculate the angle
    float dx = currentPosition.x - centreX;
    float dy = currentPosition.y - centreY;

    float newangle = atan2(dx,dy) * 180/ M_PI;

    float rotateangle = startAngle - newangle;

    //float movedAngle = rotateangle - currentAngle;

    if (rotateangle > self.currentAngle){
        clockSpin = YES;
    } else {
        clockSpin = NO;
    }

    if (rotateangle>360)
        rotateangle = rotateangle-360;

    if (rotateangle<0)
        rotateangle = fabs(rotateangle);

    self.currentAngle = rotateangle;

    lastMove = [event timestamp]; 

    [super touchesMoved:touches withEvent:event];
}

-(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    [self stop:self];
    self.spinInProgress = NO;
    [super touchesEnded:touches withEvent:event];

}

-(void)setCurrentAngle:(float)currentAngle
{

    [self willChangeValueForKey:@"currentAngle"];
    __currentAngle = currentAngle;
    [self didChangeValueForKey:@"currentAngle"];

    CGAffineTransform trans = CGAffineTransformMakeRotation(self.currentAngle*DEGREES_TO_RADIANS);

    self.transform = trans;

}

我认为最重要的一点是 setCurrentAngle ,因为它可以完成工作,但我认为我应该发布其余部分以提供一些背景信息。

您可以提供的任何帮助将不胜感激!

谢谢

理查德

4

1 回答 1

1

好的,所以终于解决了 - 事实证明自动布局和转换不能很好地一起玩(请参阅如何调整 CALayer 的锚点,当使用自动布局时?以获得很好的解释)

由于旋转,解决方案有点复杂,涉及两个步骤。

1/ 将有问题的 UIImageView 保存在另一个视图中(我在界面构建器中使用了“编辑器>嵌入>视图”) 2/将所需的自动调整大小设置为封闭视图(不是 imageView 本身) 3/设置任何自动调整掩码标志对于 UIImageView (这些将被忽略但需要在那里,以便在检测到旋转时调用 layoutSubviews)

覆盖子类 UIImageView 的 layoutSubviews 方法,如下:

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.bounds = self.superview.bounds;

}

上面的代码只是将 UIImageView 的边界设置为封闭视图的边界——这有点麻烦,但它似乎可以工作!!

希望这对某人有帮助!

理查德

于 2013-11-10T23:21:22.127 回答