I've got a UIImageView added to a UIScrollView. You can tap the scrollview and another UIImageView is added to the "main" UIImageView. I use this code to get the coordinates from the touch and convert it to a local coordinate:
- (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];
}
This works!
I use this code to rotate and scale the UIImageView:
- (void)applyTransform {
CGAffineTransform rotatetransform = CGAffineTransformMakeRotation(_rotation);
CGAffineTransform scaletransform = CGAffineTransformMakeScale(_scale, _scale);
newimageview.transform = CGAffineTransformConcat(rotatetransform,scaletransform);
}
This also works!
I use this code to move the 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;
}
This only works as long as the eye is not rotated and or scaled. If rotation is 90,180,270,360 etc. degrees and a scale of 1.f there's no problem. As soon as the scale increases and I'm moving the view the touch x,y and newimageview x,y are off. The larger the scaling the large the difference. I've tried to calculate a CGPoint with the Touch point and the transformations (rotate and scale):
CGPoint newpoint = CGPointApplyAffineTransform(newimageposition, CGAffineTransformConcat(rotatetransform,scaletransform));
But this results in a negative x...
Somehow I have to undo the rotation and scale
Position the view by setting its center
, which is invariant under transforms. Do not use its frame
.