我有一个循环 UIBezierPath
。我使用路径在我的 View 上画一个圆圈来创建一个 24 小时时钟的轮廓。我有一个 UIButton,其位置取决于当前时间。该按钮的作用类似于时针。我希望用户能够沿着圆形路径移动 UIButton。我称之为“访问 future /过去”功能。如何将按钮的移动限制在我拥有的路径上?
最佳答案
在您的 View 中重写 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.*/
}
请注意,在尝试此操作之前,您必须将圆中的所有点保存在一个数组中,否则您将必须通过知道半径来计算圆周上的点。
关于ios - 限制 UIButton 沿 UIBezierPath 路径的移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20067060/