开发者

Translating a view and the rotating it problem

开发者 https://www.devze.com 2023-03-14 07:35 出处:网络
I have a custom UIImageView, I can drag it around screen by making a translation with (xDif and yDif is the amount fingers moved):

I have a custom UIImageView, I can drag it around screen by making a translation with (xDif and yDif is the amount fingers moved):

CGAffineTransform translate = CGAffineTransformMakeTranslation(xDif, yDif);
[self setTransform: CGAffineTransformConcat([self transform], translate)]; 

Let's say I moved the ImageView for 50px in both x and y directions. I then try to rotate the ImageView (via gesture recognizer) with:

CGAffineTransform transform = CGAffineTransformMakeRotation([recognizer rotation]);
myImageView.transform = transform;

What happens is the ImageView suddenly moves to where the ImageView was originally located (before the translation - not from the moved position + 50px in both directions).

(It seems that no matter how I translate the view, the self.center of the ImageView subclass stays the same - where it was originally laid in IB).

Another problem is, if I rotate the ImageView by 30 deg, and then try to rotate it a bit开发者_StackOverflow more, it will again start from the original position (angle = 0) and go from there, why wouldn't it start from the angle 30 deg and not 0.


You are overwriting the earlier transform. To add to the current transform, you should do this –

myImageView.transform = CGAffineTransformRotate(myImageView.transform, recognizer.rotation);

Since you're changing the transform property in a serial order, you should use CGAffineTransformRotate, CGAffineTransformTranslate and CGAffineTransformScale instead so that you add to the original transform and not create a new one.

0

精彩评论

暂无评论...
验证码 换一张
取 消