2014-03-27 50 views
1

嘿傢伙我想知道是否有遠離使UIImageView可拖動的對象。我知道的UITouch和觸摸即如何使對象可拖動?

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *drag=[[event allTouches] anyObject]; 
    player.center=[drag locationInView:self.view]; 
} 

,但以這種方式,可以爲用戶進行跨屏幕中的對象跳轉到他或她觸摸屏,但我希望用戶必須手動在屏幕上拖動對象。

請及時與代碼解釋,讓我知道,如果我必須要更具體的或透明......

回答

1

你可以這樣做:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *aTouch = [touches anyObject]; 
    CGPoint location = [aTouch locationInView:self.view]; 

    if(CGRectContainsPoint(self.playerView.frame,location)) { 
     [UIView beginAnimations:@"Dragging A DraggableView" context:nil]; 
     self.playerView.center = location; 
     [UIView commitAnimations]; 
    } 

} 

這應該給你一個平滑的動畫。

+0

Thnx這樣做伎倆... – vink007

+0

如果它工作請標記爲答案:-)快樂編碼。 – CW0007007

1

,如果你希望你的用戶拖動&下降,首先你要檢查觸摸是ImageView的內矩形框架。爲了改善用戶體驗,您可以檢測來自imageview中心的觸摸偏移;一個好的地方是在the touchesBegan:withEvent:之內。在這之後,你必須改變的ImageView的位置,就像這樣:

CGPoint touchOffset; //insert this inside your interface private iVars 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *drag=[[event allTouches] anyObject]; 
    CGPoint touchLocation = [drag locationInView:self.view]; 
    touchOffset = CGPointMake(player.center.x-touchLocation.x,player.center.y-touchLocation.y) 
    if(CGRectContainsPoint(player.frame,touchLocation) 
     player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y); 
} 
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *drag=[[event allTouches] anyObject]; 
    CGPoint touchLocation = [drag locationInView:self.view]; 
    if(CGRectContainsPoint(player.frame,touchLocation) 
     player.center = CGPointMake(touchLocation.x+touchOffset.x,touchLocation.y+touchOffset.y); 
} 

附: - 下一次嘗試搜索類似的問題...

0
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch* aTouch = [touches anyObject]; 
    UIView* aView = [aTouch view]; 
    if (aView != self.view) { 
     [self.view bringSubviewToFront:aView]; 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch* aTouch = [touches anyObject]; 
    UIView* aView = [aTouch view]; 
    if (aView != self.view) { 
     aView.center = [aTouch locationInView:self.view]; 

    } 
}