2010-08-12 52 views
0

我有一个UIImageView对象,我与框架属性和CFAffineTransformMakeRotate旋转,然后我想ot移动它的框架的移动原点,但我的图像移动和strangly重塑。Draggable UIImgeView Rotation

@implementation TimberView

  • (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { // Retrieve the touch point CGPoint pt = [[touches anyObject] locationInView:self]; startLocation = pt; [[self superview] bringSubviewToFront:self]; }

  • (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { // Move relative to the original touch point CGPoint pt = [[touches anyObject] locationInView:self]; CGRect frame = [self frame];

    frame.origin.x += pt.x - startLocation.x; frame.origin.y += pt.y - startLocation.y; [self setFrame:frame]; }

的TimberView类是的UIImageView

的一个子类
+0

你可以将代码发布到移动视图的位置吗? – Vladimir 2010-08-12 09:38:03

+0

我已经发布了它。 – vahid 2010-08-14 08:18:39

回答

0

引自的UIView的帧属性参考:

如果变换属性也 集,使用的边界和中心 属性,而不是;否则, 动画更改为帧 属性不能正确反映 视图的实际位置。

因此,如果您将一些自定义转换应用于视图,则无法使用视图的框架属性。要移动视图改变其center属性代替,所以你的代码转化为(不知道如果代码正确与否):

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    // Move relative to the original touch point 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    CGPoint newCenter; 

    newCenter.x += pt.x - startLocation.x; 
    newCenter.y += pt.y - startLocation.y; 
    [self setCenter: newCenter]; 
} 

如果你只想定位视图对触摸点,您可以使用中心以下代码:

UITouch *touch = [touches anyObject]; 
CGPoint touchPoint = [touch locationInView: [self superview]]; 
self.center = touchPoint; 
+0

thnks,当我使用你的第一个代码的形状闪烁,不只是在触摸的方向移动,但你的第二代码的作品,它有一些小问题,我认为我可以解决它们。 (例如当我触摸形状上的一个点时,它的中心移动到那里,之后我可以拖动它) – vahid 2010-08-15 06:44:20