2012-02-08 79 views
0

我在我的應用程序中實現了一個可拖動的UIView。代碼我工作得很好,但我想設置一個限制區UIView可以移動。如何設置最小和最大y值?

我的代碼:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view] == camview) 
    { 
     CGPoint location = [touch locationInView:self.view; 
     startX = camview.center.x; 
     startY= location.y - camview.center.y;   
    } 
} 


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view] == camview) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     location.y =location.y - startY; 
     location.x = startX; 
     camview.center = location; 
    } 
} 

所以,我怎麼可以設置最小和最大y值UIView的可拖動?

謝謝!

回答

1

如果你有興趣在確保視圖框不走超過或低於一定的y值,你可以做到以下幾點,

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

    CGFloat minY, maxY; //The position in your self.view you are interested in 

    UITouch *touch = [[event allTouches] anyObject]; 

    if([touch view] == camview) 
    { 
     CGPoint location = [touch locationInView:self.view]; 
     location.y = location.y - startY; 
     location.y = MIN(location.y,maxY); //Always <= maxY 
     location.y = MAX(location.y,minY); //Always >= minY 
     location.x = startX; 
     camview.center = location; 
    } 
} 
+0

太謝謝你了(! – Grace 2012-02-08 03:24:11

相關問題