在旋转图像后更改设备方向时,UIImageView会被拉伸

时间:2013-11-08 23:11:05

标签: ios objective-c uiimageview orientation

我已经将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,因为它可以完成工作,但我认为我应该发布其余内容以提供一些背景信息。

非常感谢您提供的任何帮助!!

由于

理查德

1 个答案:

答案 0 :(得分:1)

好的,所以最终解决了 - 事实证明自动布局和转换不能很好地协同工作(请参阅How do I adjust the anchor point of a CALayer, when Auto Layout is being used?以获得一个很好的解释)

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

1 /在另一个视图中保存有问题的UIImageView(我在界面构建器中使用'editor&gt;嵌入&gt;视图') 2 /将所需的自动调整设置为封闭视图(不是imageView本身) 3 /为UIImageView设置任何自动化屏蔽标志(这些将被忽略,但需要在那里,以便在检测到旋转时调用layoutSubviews)

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

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

}

上面的代码只是简单地将UIImageView的边界设置为封闭视图的边界 - 这有点像一个小屋,但它似乎工作!!

希望这对某人有帮助!

理查德