开发者

how can I make the mouse to position at clicked position of the imageview.?

开发者 https://www.devze.com 2023-01-22 16:00 出处:网络
While I drag an uiimageview, the center is positioned at clickpoint..I amusing the following code imageView.center=[[touches anyObject] locationInView:self.view];

While I drag an uiimageview, the center is positioned at clickpoint..I am using the following code

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

so the mouse jumps to image center even I drag at the corner.

How can I make th开发者_开发问答e mouse to position at clicked position of the imageview.???

Thanx in advance


I'm supposing you want to move your imageView and the imageView is moving its center to your touch point "unintentionally"...

If you don't want your imageView to set its center at the touch but be dragged from any point the user touhes it, keep track of the first CGPoint he touched, and then reposition your image at the relative distance between the previous two touchpoints

@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

精彩评论

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