【问题标题】:How to detect touch on view during animation and i have used UIViewAnimationOptionAllowUserInteraction property but still i am not getting the touch如何在动画期间检测视图上的触摸,我使用了 UIViewAnimationOptionAllowUserInteraction 属性,但我仍然没有得到触摸
【发布时间】:2013-08-14 06:15:27
【问题描述】:

我已经选择了两个按钮,它们从左到右连续移动,但是当我试图单击其中一个时。即使我在动画上设置了 UIViewAnimationOptionAllowUserInteraction 属性,我也无法检测到单击了哪个按钮,但它对我不起作用。

{
m_pFacebookPostButton=[UIButton buttonWithType:UIButtonTypeCustom];
m_pFacebookPostButton.frame=CGRectMake(-320, 410, 320, 50);
[m_pFacebookPostButton retain];
[m_pFacebookPostButton setBackgroundColor:[UIColor greenColor]];
m_pFacebookPostButton.tag=204;
[m_pFacebookPostButton setTitle:@"Facebook" forState:UIControlStateNormal];
//[m_pFacebookPostButton addTarget:self action:@selector(clickone) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_pFacebookPostButton];

m_pFacebookPostButton1=[UIButton buttonWithType:UIButtonTypeCustom];
m_pFacebookPostButton1.frame=CGRectMake(0, 410, 320, 50);
[m_pFacebookPostButton1 retain];
[m_pFacebookPostButton1 setBackgroundColor:[UIColor redColor]];
m_pFacebookPostButton1.tag=205;
[m_pFacebookPostButton1 setTitle:@"Twitter" forState:UIControlStateNormal];
//[m_pFacebookPostButton1 addTarget:self action:@selector(clicktwo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_pFacebookPostButton1];

[UIView animateWithDuration:5.0
                      delay:0.0
                    options:(UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction)
                 animations:^{
                     [UIView setAnimationDelegate:self];
                     [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];


                      m_pFacebookPostButton.frame = CGRectMake(0, 410, 320, 50);
                      m_pFacebookPostButton1.frame = CGRectMake(320, 410, 320, 50);



                 }
                 completion:^(BOOL finished){
                     NSLog(@"Move to left done");
                 }];

}

请帮我解决这个问题,谢谢提前帮助我。

【问题讨论】:

  • 看看我编辑的答案...

标签: objective-c animation ios5 event-handling


【解决方案1】:

可以创建自定义按钮对象(New File > SubClass of: UIButton)并添加到底部

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(self.bounds, point)) {
        NSLog("Button got hit");
        return self;
    }
    else {
        return nil
    }
}

并在您的 viewController 中使用此自定义按钮

myCustomButton * m_pFacebookPostButton = [[myCustomButton alloc] init];

【讨论】:

    【解决方案2】:

    似乎是关于 Quartz2D 的问题。

    参考这个==> Is it possible to successful animate a moving UIButton?

    Engin Kurutepe 表示,

    This sounds a bit too complicated for UIKit and CoreAnimation. It seems like you're developing something like a game. Why not use a global animation timer for let's say ~60 fps and do your drawing in Quartz2D? Then for each touch you could quickly check any hits between Quartz refreshes.
    

    编辑:尝试使用其他动画方法。

    例子:

    -(void)hello
    {
        NSLog(@"hello");
    
    }
    -(void)animationDone
    {
        NSLog(@"done");
    }
    
    UIButton *button=[[UIButton alloc]init];
    button.frame = CGRectMake(30, 50, 100, 100);
    button.backgroundColor=[UIColor redColor];
    [button addTarget:self action:@selector(hello) forControlEvents:UIControlEventTouchUpInside];
      [self.view addSubview:button];
    // animate
    [UIView beginAnimations:@"button_in" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDone)];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [button setFrame:CGRectMake(100.0, 100.0, 100.0, 30.0)];
    [UIView commitAnimations];
    

    希望对你有帮助...

    【讨论】:

      猜你喜欢
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多