使用CAKeyframeAnimation旋转imageView箭头,并使箭头响应触摸事件

时间:2011-10-13 19:08:16

标签: iphone objective-c ios xcode

当用户使用CAKeyframeAnimation单击按钮时,我有一个旋转的箭头。

箭头指向的位置决定了用户看到的图像。

这很好用。

随机选择旋转的最终位置,并在用户点击按钮时为其设置下一个图像。

-(void)rotateAnimation
{


    [self setDialStartPosition];
    [self setDialEndPosition];


    //Spin the Arrow

    CALayer *layer = imageView.layer;
    CAKeyframeAnimation *animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.duration = 8;
    animation.cumulative = NO;
    animation.repeatCount = 0;
    animation.values = [NSArray arrayWithObjects:           // i.e., Rotation values for the 3 keyframes, in RADIANS
                        [NSNumber numberWithFloat:[self dialStartPosition] * M_PI], 
                        [NSNumber numberWithFloat:2.5 * M_PI], 
                        [NSNumber numberWithFloat:[self dialEndPosition] * M_PI], nil]; 
    animation.keyTimes = [NSArray arrayWithObjects:     // Relative timing values for the 3 keyframes
                          [NSNumber numberWithFloat:0], 
                          [NSNumber numberWithFloat:.055], 
                          [NSNumber numberWithFloat:1], nil]; 
    animation.timingFunctions = [NSArray arrayWithObjects:
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],
                                 // from keyframe 1 to keyframe 2
                                 [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut], nil]; // from keyframe 2 to keyframe 3
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;

    [layer addAnimation:animation forKey:nil];
    ....

我有这个工作,但我希望用户能够通过触摸手动移动箭头,并且当他们移动时,箭头指向他们的手指方向。

当他们抬起手指并按下按钮时,我希望箭头的位置决定用户随后看到的图像。所以touchesEnded必须更新dialStartPosition。

这是我的触摸代码..

double wrapd(double _val, double _min, double _max)
{
    if(_val < _min) return _max - (_min - _val);
    if(_val > _max) return _min - (_max - _val);
    return _val;
}
    - (void) touchesBegan:(NSSet *)_touches withEvent:(UIEvent *)_event
    {

        UITouch* touch = [_touches anyObject];
        CGPoint location = [touch locationInView:self.view];
        m_locationBegan = location;
    }

    - (void) touchesMoved:(NSSet *)_touches withEvent:(UIEvent *)_event
    {

        UITouch* touch = [_touches anyObject];
        CGPoint location = [touch locationInView:self.view];
        [self updateRotation:location];
    }

    - (void) touchesEnded:(NSSet *)_touches withEvent:(UIEvent *)_event
    {
        UITouch* touch = [_touches anyObject];
        CGPoint location = [touch locationInView:self.view];
        m_currentAngle = [self updateRotation:location];

    }

    - (float) updateRotation:(CGPoint)_location
    {
        float fromAngle = atan2(m_locationBegan.y-imageView.center.y, m_locationBegan.x-imageView.center.x);
        float toAngle = atan2(_location.y-imageView.center.y, _location.x-imageView.center.x);
        float newAngle = wrapd(m_currentAngle + (toAngle - fromAngle), 0, 2*3.14);

        CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
        imageView.transform = cgaRotate;

        return newAngle;
    }

Heres现在正在讨厌......

在我按下按钮之前,我可以按照我想要的方式移动箭头。

当我按下按钮时,它没有响应我通过触摸手动设置位置的位置。

一旦CAKeyframeAnimation播放,它就不再响应任何触摸,除非我改变

animation.fillMode = kCAFillModeForwards;

animation.fillMode = kCAFillModeRemoved;

但如果我这样做,它会在动画运行后删除动画,并在我手动设置动画时显示箭头。

我是一个很新的节目,所以任何帮助都会受到赞赏

由于

0 个答案:

没有答案