我需要防止視圖被拖過屏幕邊緣。換句話說,當視圖的邊緣到達屏幕的邊緣時,視圖無法在該方向上繼續移動。防止視圖被拖出屏幕
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touchInfo = [touches anyObject];
if (touchInfo.view == self)
{
CGPoint touchStart = [touchInfo previousLocationInView:self];
CGPoint touchEnd = [touchInfo locationInView:self];
CGFloat xDifference = touchEnd.x - touchStart.x;
CGFloat yDifference = touchEnd.y - touchStart.y;
CGPoint newCenter = CGPointMake(self.center.x + xDifference, self.center.y + yDifference);
[self setCenter:newCenter];
}
}
如何檢查頂部,底部和左邊? – jipot
如果您不確定如何檢查每個邊,您可能需要閱讀UIKit座標系。起點是左上角。 –