2014-04-03 70 views

回答

1

只是聲明

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; 
} 
+0

正是我想要的!非常感謝您的快速回答! – Raghu