旋转和缩放后移动 UIImageView 时的奇怪行为 x,y 坐标
Strange behavior x,y coordinates when moving UIImageView after rotating and scaling
我已经将 UIImageView 添加到 UIScrollView。您可以点击滚动视图,另一个 UIImageView 将添加到 "main" UIImageView。我使用这段代码从触摸中获取坐标并将其转换为本地坐标:
- (void)tapped:(UITapGestureRecognizer *)gestureRecognizer {
CGPoint position = [gestureRecognizer locationInView:_imagescrollview];
CGPoint positioninimageview = [_imagescrollview convertPoint:position toView:_editimageview];
newimageview.frame = CGRectMake(positioninimageview.x, positioninimageview.y,160.f,160.f);
[self.editimageview addSubview:newimageview];
}
这有效!
我使用这段代码来旋转和缩放 UIImageView:
- (void)applyTransform {
CGAffineTransform rotatetransform = CGAffineTransformMakeRotation(_rotation);
CGAffineTransform scaletransform = CGAffineTransformMakeScale(_scale, _scale);
newimageview.transform = CGAffineTransformConcat(rotatetransform,scaletransform);
}
这也行!
我使用这段代码来移动 UIImageView:
- (void)setEyePositionAndReposition:(CGPoint)eyeposition {
CGAffineTransform t = newuiimageview.transform;
newuiimageview.transform = CGAffineTransformIdentity;
newuiimageview.frame = CGRectMake(eyeposition.x, eyeposition.y, _eyeimageview.frame.size.width, _eyeimageview.frame.size.height);
newuiimageview.transform = t;
}
这仅在眼睛未旋转和/或缩放时有效。如果旋转是 90,180,270,360 等度和 1.f 的比例则没有问题。一旦比例增加并且我正在移动视图,touch x,y 和 newimageview x,y 就会关闭。缩放比例越大,差异越大。我尝试用接触点和变换(旋转和缩放)计算 CGPoint:
CGPoint newpoint = CGPointApplyAffineTransform(newimageposition, CGAffineTransformConcat(rotatetransform,scaletransform));
但这会导致负 x...
不知何故我必须撤消旋转和缩放
通过设置其 center
来定位视图,这在变换下是不变的。不要使用它的 frame
.
我已经将 UIImageView 添加到 UIScrollView。您可以点击滚动视图,另一个 UIImageView 将添加到 "main" UIImageView。我使用这段代码从触摸中获取坐标并将其转换为本地坐标:
- (void)tapped:(UITapGestureRecognizer *)gestureRecognizer {
CGPoint position = [gestureRecognizer locationInView:_imagescrollview];
CGPoint positioninimageview = [_imagescrollview convertPoint:position toView:_editimageview];
newimageview.frame = CGRectMake(positioninimageview.x, positioninimageview.y,160.f,160.f);
[self.editimageview addSubview:newimageview];
}
这有效!
我使用这段代码来旋转和缩放 UIImageView:
- (void)applyTransform {
CGAffineTransform rotatetransform = CGAffineTransformMakeRotation(_rotation);
CGAffineTransform scaletransform = CGAffineTransformMakeScale(_scale, _scale);
newimageview.transform = CGAffineTransformConcat(rotatetransform,scaletransform);
}
这也行!
我使用这段代码来移动 UIImageView:
- (void)setEyePositionAndReposition:(CGPoint)eyeposition {
CGAffineTransform t = newuiimageview.transform;
newuiimageview.transform = CGAffineTransformIdentity;
newuiimageview.frame = CGRectMake(eyeposition.x, eyeposition.y, _eyeimageview.frame.size.width, _eyeimageview.frame.size.height);
newuiimageview.transform = t;
}
这仅在眼睛未旋转和/或缩放时有效。如果旋转是 90,180,270,360 等度和 1.f 的比例则没有问题。一旦比例增加并且我正在移动视图,touch x,y 和 newimageview x,y 就会关闭。缩放比例越大,差异越大。我尝试用接触点和变换(旋转和缩放)计算 CGPoint:
CGPoint newpoint = CGPointApplyAffineTransform(newimageposition, CGAffineTransformConcat(rotatetransform,scaletransform));
但这会导致负 x...
不知何故我必须撤消旋转和缩放
通过设置其 center
来定位视图,这在变换下是不变的。不要使用它的 frame
.