1

我正在尝试创建一个挥杆计,有点像高尔夫游戏中使用的挥杆计,用户按住按钮开始挥杆,在顶点释放以确定力量,此时“挥杆”反转方向,并且用户在向下的途中再次点击以设置准确性。

我不想使用一个可以填满的条,而是使用一个从左到右摆动的钟摆。摆锤在第一次敲击的地方留下一个标记(功率),在第二次敲击的地方留下另一个标记(准确度)。

我的第一个问题是旋转钟摆。我一直在阅读这些关于旋转 UIImageView 的帖子——这里这里这里,但我希望我能得到一些额外的建议:)

首先,旋转 UIImageView 是解决这个问题的最佳方法吗?我正在考虑通过将钟摆的坐标与正确点的坐标进行比较来确定钟摆与正确点的距离。但要使其正常工作, UIImageView 必须物理移动,我不确定它是否会移动?此外,如何使 UIImageView 围绕特定点而不是中心点旋转。或者我应该只是让钟摆的边界框更大,这样中心点就在正确的位置。我怎样才能在用户点击(或释放)按钮的地方留下“印章”?

任何有关解决此问题的最佳方法的一般帮助将不胜感激:) 也许我的问题有点模糊和笼统,但我希望有人会喜欢这个挑战:)

谢谢!!

迈克尔

更新:

好的,我尝试了以下两种方法,现在这两种方法都有效。

    - (void)viewDidLoad {
        pendulum.image = [UIImage imageNamed:@"winkOpen.png"];
        [self swingPendulum];
    }

    - (void)swingPendulum {
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:degreesToRadian(180)]; 
    rotationAnimation.duration = 0.75;
    rotationAnimation.repeatCount = 1.0; 
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    [pendulum.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}


    /*
    - (void)swingPendulum {
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:1];
        pendulum.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
    }
    */

现在我不确定哪种方法更好,但是第一种方法,钟摆摆动高于 0 度而不是低于它,所以我会这样做。

现在我需要确定钟摆在哪里停止并扭转摆动......但也许我会将这些作为单独的问题发布,因为这有点混乱!我会确保我的问题在未来更加具体:)

4

1 回答 1

1

对于动画,您需要查看变换。请参阅Quartz 2D 编程指南:Transforms。

您不应使用视图的实际像素位置进行计算。如果这样做,屏幕尺寸和/或分辨率的更改将需要完全重写。而是创建运动和位置的抽象数据模型,在抽象模型中执行计算。然后将抽象坐标转换为当前显示的坐标。

与首先计算它应该在哪里相比,让 imageView 出现在您想要的位置是相对微不足道的。你必须让动作正确。记住,“如果你没有挥杆,那就没有意义了!”

于 2010-08-10T12:47:58.200 回答