我有一个圆形UIBezierPath。我使用路径在我的视图上画一个圆圈,创建一个24小时钟的轮廓。我有一个UIButton,它的位置取决于当前的时间。这个按钮就像一只时针。我希望用户能够沿着循环路径移动UIButton。我称之为“访问未来/过去”功能。如何将按钮移动限制在我的路径上?
发布于 2013-11-19 10:18:59
在视图中重写touchesBegan:和touchesMoved:方法
- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event
{
if([[event touchesForView:button] count])
{
//User is trying to move the button set a variable to indicate this.
}
}
- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint *point = [[event anyObject] locationInView:self];
/*Compare x and y coordinates with the centre property of the button
If x or y are greater set center of button to next point in circle or previous
point if any of them are lesser.*/
}注意,在尝试之前,您必须将圆圈中的所有点保存在数组中,或者必须通过知道圆周的半径来计算圆周上的点。
发布于 2013-11-19 09:58:58
最简单的方法是在touchesMoved中,您可以通过以下方法检查忽略不在圆圈视图中的触摸:
CGPoint point = [touch locationInView:circleView];
if (![circleView pointInside:point withEvent:event]) {
return;
}https://stackoverflow.com/questions/20067060
复制相似问题