2013-11-27 41 views
0

期间,我创建下面的UITouch点的UIButton,我想,当我继续按触摸和动画界达到我的触摸点我执行一些活动存在。CGRectContainsPoint为一个UIButton动画

以下是验证码。

我总是得到x,y 0为UIButton。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [UIView beginAnimations:@"MoveAndStrech" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    if([touches count] == 1) { 
     UITouch *touch = [touches anyObject]; 
     self.uiButton.center = [touch locationInView:self.view]; 
    } 
    [UIView commitAnimations]; 

    UITouch *touchTemp = [touches anyObject]; 
    CGPoint pCurrent = [touchTemp locationInView:self.view]; 
    CGRect currentRect = self.uiButton.layer.bounds; 

    NSLog(@"Current Rect x:%f,Current y:%f", 
      currentRect.origin.x, 
      currentRect.origin.y); 
    NSLog(@"TouchPoint x:%f,TouchPoint y:%f", 
      pCurrent.x, 
      pCurrent.y); 

    if(CGRectContainsPoint(currentRect, pCurrent)) { 
     NSLog(@"Touched....in rect"); 
    } 
} 

问候 萨拉

回答

0

更换

CGRect currentRect = self.uiButton.layer.bounds; 

CGRect currentRect = self.uiButton.frame; 

如果还是不行,那么

CGRect currentRect = self.uiButton.presentationLayer.frame; 

另外,为什么不使用UIView动画块?它会让你的生活变得更容易,你的代码更加干净。

+0

如果我使用self.uiButton.frame;它使我喜欢一旦我点击它给我一个CGRectContainsPoint为真如果我使用CGRect currentRect = self.uiButton.presentationLayer.frame;它给了我一个错误,没有找到属性 – user3042604

+0

所以使用'layer.presentationLayer'。去做一些研究,哥们。 –

0

几件事情。起初我以为你错过了动画提交动画。

不要把在同一行上多个语句!!!!这是一个非常令人讨厌的编码习惯,使得你的代码几乎不可能读取。

接下来,老式beginAnimations/commitAnimations电话已经过时,不应该在的iOS 4或更高版本中使用。引用beginAnimations的文档:上下文:

在iOS 4.0及更高版本中不鼓励使用此方法。您应该使用基于块的动画方法 来指定您的动画。

你应该使用新的基于块的动画调用(形式为(animateWithDuration:动画:)

接下来,UIView的动画导致对象被动画移动到它瞬间的最终位置,他们只看起来像他们正在移动到他们的最终位置,他们也禁止在动画默认情况下的用户交互

如果你想弄清楚视图出现在屏幕上的任何时候,那么你需要检查视图的图层的animationLayer。我有一个sample project on Github(链接),其中包括一个图像的UIView动画您可以点击图像并停止动画。该示例项目演示了如何在用户点击屏幕时使用视图的图层动画层来确定动画的位置。

如果你想代码做一些事情,当动画视图到达某一点会比较棘手。您可能需要将CADisplayLink附加到视图(将在每个屏幕刷新时调用该视图),并使用该视图检查动画的位置并将其与当前触摸位置进行比较。

+0

检查样品,它确实告诉事情,但仍然有任何替代一个快速的一个,这样当视图到达触摸点它发送一个信号!或者可能会引用我从动画块开始的一些内容,并了解如何才能完成此操作! – user3042604