gpt4 book ai didi

ios - 旋转图像后更改设备方向时,UIImageView 变得非常拉伸(stretch)

转载 作者:行者123 更新时间:2023-11-29 13:00:41 33 4
gpt4 key购买 nike

我已经子类化了 UIImageView 以允许我旋转图像,或者用用户的手指拖动图像,或者使用 slider 来设置图像旋转。

这在最初使用时一切正常,但是如果用户旋转图像然后重新定位设备,则图像大小会变得非常糟糕 - 看起来如果用户顺时针旋转图像然后图像会变大,逆时针方向图像变小。

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

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

- (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,因为它起作用了,但我认为我应该发布其余部分以提供一些背景信息。

如果您能提供任何帮助,我们将不胜感激!

谢谢

理查德

最佳答案

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

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

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

重写子类 UIImageView 的 layoutSubviews 方法如下:

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

}

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

希望对大家有帮助!

理查德

关于ios - 旋转图像后更改设备方向时,UIImageView 变得非常拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19870170/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com