-2
我的應用程序有一個UIView,其中有一個UIImageView。用戶需要能夠移動該圖像,但不一定要觸摸圖像。拖動UIImageView而不必觸摸圖像?
我的應用程序有一個UIView,其中有一個UIImageView。用戶需要能夠移動該圖像,但不一定要觸摸圖像。拖動UIImageView而不必觸摸圖像?
只是聲明
NSInteger offsetX;
NSInteger offsetY;
,實施您的視圖控制器下面的方法:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get touch coordinates in location view
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
//Calculate offset on touches began
offsetX = touchPoint.x - imageView.frame.origin.x;
offsetY = touchPoint.y - imageView.frame.origin.y;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get touch coordinates in location view
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
//Create new frame with origin offset
CGRect newFrame = CGRectMake(touchPoint.x - offsetX, touchPoint.y - offsetY, imageView.bounds.size.width, imageView.bounds.size.height);
//Set the new frame
imageView.frame = newFrame;
}
正是我想要的!非常感謝您的快速回答! – Raghu