2013-07-23 17 views
0

當我拖動屏幕上的uiimageview時,發生了我的問題,它被設置爲只能在x軸方向上拖動。 作品的代碼種類。 uiimageview正在移動,它僅限於x軸,這正是它應該的。 但是當你開始在uiimageview的框架外面拖動時,它會停止沿着我的手指移動。關於TouchesMoved使用方法的問題CGRectContainsPoint

這種遺忘與此方法有關:CGRectContainsPoint。 在我的代碼中,它是非常必要的,因爲我只希望uiimageview能夠在用戶將手指放在它上面時移動。

如果我沒有使用這種方法CGRectContainsPoint,即使用戶手指不接觸圖像,圖像仍然會移動。任何有關這方面的工作都非常感謝。

這裏是我的代碼:

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

NSLog(@"Touches Moved is running"); 

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:self.view]; 

if (CGRectContainsPoint(UIImageView, location) && UIImageView >= 40) 
{ 


    NSLog(@"Contains Point UIImageView Center!"); 

    CGPoint xLocation = CGPointMake(location.x,UIImageView); 
    UIImageView = xLocation; 

    //here it comes.. big block of code// 
    if (location.x <= 40) { 

     NSLog(@"Start Dragging Point"); 

     CGPoint newLocation = CGPointMake(40 
              , 402); 

     UIImageView = newLocation; 
    } 

    else if(location.x >= 273) { 

     NSLog(@"End Dragging Point"); 

     CGPoint newLocation = CGPointMake(273 
              , 402); 

     UIImageView = newLocation; 

    } 
} 
+0

那麼最新的問題? –

+0

問題是,只要我拖動uiimageview,並且我的手指伸到uiimageview的框架之外,它就會停止移動,但實際上它不應該移動。它應該像ios 6幻燈片一樣解鎖主屏幕功能。 –

回答

0

移動CGRectContainsPoint(UIImageView, location)-(void)touchesMoved:...-(void)touchesBegan:...

在那裏設置一個ivar,只有在視圖內部開始觸摸時才指向視圖。從-(void)touchesMoved:...使用此ivar無限移動視圖。然後您可以在-(void)touchesEnded:...-(void)touchesCancelled:...中取消設置該變量。