我有UIView,我必须在圆形路径中移动它。UIView在圆形路径中移动(不是动画)
-4
A
回答
3
简单。将UIImageView添加到UIView的子类,该子类具有图像的属性,以便您可以在代码中移动它。实现touchesBegan:... touchesMoved:...和touchesEnded:...将图像移动到圆上适当的点。这里有一些简单的数学:
编辑:添加了一些评论,并修复了象限错误。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
CGPoint viewCenter = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
CGPoint imageOrigin = self.imageOnCircle.frame.origin;
CGSize imageSize = self.imageOnCircle.frame.size;
CGPoint imageCenter = CGPointMake(imageOrigin.x + imageSize.width/2,
imageOrigin.y + imageSize.height/2);
CGFloat xDist = imageCenter.x - viewCenter.x;
CGFloat yDist = imageCenter.y - viewCenter.y;
CGFloat radius = sqrt(xDist*xDist + yDist*yDist);
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CGFloat touchXDist = touchPoint.x - viewCenter.x;
CGFloat touchYDist = touchPoint.y - viewCenter.y;
// angles in the view coordinates are measured from the positive x axis
// positive value means clockwise rotation
// -π/2 is vertically upward (towards the status bar)
// π/2 is vertically downward (towards the home button)
CGFloat newAngle = atanf(touchYDist/touchXDist);
// arctan takes a value between -π/2 and π/2
CGFloat newXDist = radius * cosf(newAngle);
// cos has a value between -1 and 1
// since the angle is between -π/2 and π/2, newXDist will always be positive.
if (touchXDist < 0)
newXDist *= -1;
CGFloat newYDist = radius * sinf(newAngle);
// sin has a value between -1 and 1
// since the angle is between -π/2 and π/2, newYDist can attain all its values.
// however, the sign will be flipped when x is negative.
if (touchXDist < 0)
newYDist *= -1;
CGPoint newCenter = CGPointMake(viewCenter.x + newXDist,
viewCenter.y + newYDist);
CGPoint newOrigin = CGPointMake(newCenter.x - self.imageOnCircle.frame.size.width/2,
newCenter.y - self.imageOnCircle.frame.size.height/2);
self.imageOnCircle.frame = CGRectMake(newOrigin.x,
newOrigin.y,
self.imageOnCircle.frame.size.width,
self.imageOnCircle.frame.size.height);
}
此外,您可能想添加一个最大/最小角度,以运动限制一方或另一方...
相关问题
- 1. 在圆形路径中移动uiview
- 2. 在圆形路径中移动JLabel
- 3. 动画效果GMSMarker圆形路径
- 4. 在圆形路径上移动画布图像
- 5. CGAffineTransformMakeScale圆形UIView动画为正方形
- 6. 在滚动条上沿圆形路径移动div
- 7. 圈的UIView动画保持圆形状
- 8. 在圆形路径中如何进行动画uibuttons(目标C)
- 9. 在正弦波路径上动画UIView
- 10. Java:在圆形路径中移动标签
- 11. 如何使对象在圆形路径中移动?
- 12. 如何使用jquery在圆形路径中移动图像?
- 13. Cocos2d在路径上移动动画
- 14. 圆形动画
- 15. 圆形动画
- 16. 圆形动画
- 17. 拖动在圆形内移动图像(圆形移动)
- 18. 在圆形路径上拖动一个画布元素
- 19. 沿路径移动形状?
- 20. SVG - 沿着路径的动画矩形;矩形中心总是在路径上
- 21. javafx鼠标在圆形路径上移动
- 22. 在圆形路径上移动一个点
- 23. 动画svg图像:得到的路径动画,但不是椭圆
- 24. CAShapeLayer路径动画 - 缩小圆圈
- 25. Paper.js动画上点击路径移动
- 26. CAKeyframeanimation沿着路径移动UIView
- 27. 沿着D3路径移动一个圆圈以不同的速度动画
- 28. 统一5移动行星圆形或椭圆形的路径(轨道)
- 29. 移动路径上的对象(圆圈)
- 30. UIView动画不会移动视图
好知道... – 2013-04-27 18:04:30
OK,现在你已经告诉我们想要做什么,请更新您的问题以提出实际问题。你试过什么了?你到底需要什么帮助?请记住,你需要问一个具体的编程问题。 – rmaddy 2013-04-27 18:25:52
...这个问题对我来说似乎没问题... – samson 2013-04-27 19:24:13