如何设置最小和最大y值?
问题描述:
我在我的应用程序中实现了一个可拖动的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的可拖动?
谢谢!
答
如果你有兴趣在确保视图框不走超过或低于一定的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;
}
}
太谢谢你了(! – Grace 2012-02-08 03:24:11