2010-11-01 115 views
-1

虽然我拖动uiimageview,该中心位于clickpoint..I现在用下面的代码如何让鼠标在imageview的点击位置上定位?

 
imageView.center=[[touches anyObject] locationInView:self.view]; 

所以鼠标跳到图像中心连我拖动在拐角处。

如何让鼠标定位在imageview的点击位置?

提前

+2

呃,在iOS中有一个鼠标?我不知道。 – 2010-11-01 05:52:02

回答

2

我假设你想要移动的ImageView和ImageView的移动中心,以你的触摸点“无意”感谢名单...

如果您不希望您的ImageView设置的中心位于触摸而从任意点拖动的用户touhes,不停地摸他的第一个CGPoint的轨道,然后在相对距离重新定位的前两个接触点之间的图像

@interface myViewController : UIViewController { 
    CGPoint touchPoint; 
    BOOL touchedInside; 
} 
@end 

@implementation myViewController 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    touchPoint = [touch locationInView:self.view]; 
    CGPoint pointInside = [touch locationInView:imageView]; 
    if ([imageView pointInside:pointInside withEvent:event]) 
     touchedInside = YES; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (touchedInside) { 
     UITouch *touch = [touches anyObject]; 
     CGPoint newPoint = [touch locationInView:self.view]; // get the new touch location 
     imageView.center = CGPointMake(imageView.center.x + newPoint.x - touchPoint.x, imageView.center.y + newPoint.y - touchPoint.y); // add the relative distances to the imageView.center 
     touchPoint = newPoint; // assign the newest touch location to the old one 
    } 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
    touchedInside = NO; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    if (touchedInside) { 
     UITouch *touch = [touches anyObject]; 
     CGPoint newPoint = [touch locationInView:self.view]; 

     imageView.center = CGPointMake(imageView.center.x + newPoint.x - touchPoint.x, imageView.center.y + newPoint.y - touchPoint.y); 
    } 
    touchedInside = NO; 
} 

@end 
+0

thanx .....它工作:) – Jeff 2010-11-01 10:30:11

+0

np,不要忘记标记答案为正确和/或投票(如果它真的是!),声誉鼓励贡献:) – tsakoyan 2010-11-01 10:38:15