2012-06-19 57 views
2

当使用基于块的动画的选项字段进行动画时,允许用户与视图交互是非常容易的。但在我的程序中,我使用的是CAKeyframeAnimation,而且我没有看到任何属性来设置启用的用户交互。有没有办法做到这一点?使用CAAnimation支持用户交互?

感谢,

编辑:下面是实际的代码:

- (void)move 
{ 
    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathMoveToPoint(path, NULL, 50, 120); 
    for (int i = 0; i < 5; i ++) 
     CGPathAddLineToPoint(path, NULL, arc4random() % 320, arc4random() % 480); 
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
    [animation setPath:path]; 
    [animation setDuration:10]; 
    CFRelease(path); 
    [self.layer addAnimation:animation forKey:@"move"]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self setAlpha:0]; 
} 
+0

它是视图的一个属性。你应该可以在mainView中设置它。 – Legolas

+0

Legolas,我试过了,无济于事。感谢 –

+0

能否详细说明你想要达到的目标? – Legolas

回答

1

你最有可能接触到错误的地方。当图层在屏幕上动画时,值永远不会改变,因此视图实际上定位在从开始的位置,而不是它出现在屏幕上的位置。

我几个月前在how to hit test animating layers上做了一篇博文。它描述了你正在尝试做的确切的事情。

您需要将触摸处理(或手势识别器)添加到超级视图中,并自己执行presentationLayer的命中测试,以确定用户是否轻敲视图出现在屏幕上的位置。

+0

中发布我的代码。感谢David,你的文章似乎正好碰到了我想知道的一点。但是,我不想使用手势识别器,因为我希望直接触摸到结果。有没有办法做到这一点? –

+0

是的,我同意戴维的评论。你实际上没有玩正确的坐标。为什么不试图显示你正在触摸的地方的坐标,以及多维数据集的位置,并且你可以理解很多。 – Legolas

+0

David,我刚刚意识到你在vc中实现了手势识别器。这应该适用于我。谢谢! –

0

显然,预览解决方案不再斯威夫特3.x和4.x版雨燕

工作我解决了问题,设置定时器来更新动画过程中框。

//Timer: To update the UI frame 
    Timer.scheduledTimer(timeInterval: 0.15, target: self, selector: #selector(updateFrame), userInfo: nil, repeats: true) 

//Timer Selector 
@objc func updateFrame(){ 
    //getting the layer presentation bounds 
    let layer = "button, label...".layer.presentation()! 

    let point = CGPoint(x: layer.frame.origin.x, y: layer.frame.origin.y) 
    let size = CGSize(width: layer.frame.width, height: layer.frame.height) 
    // Update the UI element frame location 
    "ui element".frame = CGRect(origin: point, size: size) 
}