2013-07-12 64 views
0

我有1年的Objective-C工作经验,是我做的第一个问题,因为我总是在这个网站上找到答案,但这种情况完全是疯了,没有发现任何类似的东西。UITapGesture只有在重新加载UIViewController后才能正常工作

我有一个自定义类(从UIView称为BallClass)。 UIView里面有一个UIImageView和一个UITapGesture来做一个动作。

因此,当我在UIViewController中创建对象时,按下对象时不起作用tapAction,但如果我切换到其他屏幕并返回它,tapGesture的工作就非常完美。

我尝试了很多选择,但我无法在第一次加载中工作。我使用ARC,Xcode(4.6.3)和OSX(10.8.4)和IB工作,但是这个对象是在没有IB的情况下创建的。

在此先感谢。

这里是类BallClass.m

- (void)tapGesture:(UIGestureRecognizer *)tapGesture { 
      NSLog(@"hola"); 
    } 

    - (id)initBall:(CGRect)frame { 
      if (self = [super initWithFrame:frame]) { 
      // Initialization code 

      // Recibir las variables iniciales para el control del timer 
      ball = [[Ball alloc] init]; 

      // Agregar la imagen de la pelota en el view 
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height)]; 
      [imageView setCenter:CGPointMake(CGRectGetMidX([self bounds]), CGRectGetMidY([self bounds]))]; 
      [imageView setContentMode:UIViewContentModeScaleAspectFit]; 
      [imageView setImage:[UIImage imageNamed:@"pelota.png"]]; 
      [imageView setTag:100]; 
      [imageView setNeedsDisplay]; 

     // Add TAP 
      UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; 
     // Tap Properties 
     [tapGesture setDelaysTouchesBegan:YES]; 
     [tapGesture setNumberOfTapsRequired:1]; 
     [tapGesture setDelegate:(id)self]; 
     [imageView addGestureRecognizer:tapGesture]; 

     [self addSubview:imageView]; 

     [self setNeedsDisplay]; 

     // Propiedades del View 
     [self setUserInteractionEnabled:YES]; 
     self.backgroundColor = [UIColor clearColor]; 
     [self setTag:100]; 

     // BALL BOUNCE 
     [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];   
     } 

     return self; 
     } 

    - (void)onTimer:(NSTimer *)timerLocal { 
     // Do something 
    } 

    ... // more methods  

这里是实例化:

 ballView00 = [[BallView alloc] initBall:CGRectMake(10, 10, 40, 40)]; 

UIView的完美展现,和动画工作正常,但UITapGesture只是工作,早提及,重新加载ViewController后。

这里是图像Ball和UIView(@kBall)的链接。对不起,有关图像,但我不能负荷,直到我有10分:(

回答

0

我找到了解决方案。

球使本旋转:

[UIView animateWithDuration: 0.5f 
         delay: 0.0f 
        options: UIViewAnimationOptionCurveEaseIn 
       animations: ^{ 
        self.transform = CGAffineTransformRotate(self.transform, M_PI/2); 
       } 
       completion: ^(BOOL finished) { 
        if (finished) { 
         if (animating) { 
          // if flag still set, keep spinning with constant speed 
          [self spinWithOptions: UIViewAnimationOptionCurveLinear]; 
         } 
        } 
       }]; 

,并产生与主计时器冲突。我用简单的add self.transform = CGAffineTransformRotate(self.transform,M_PI/2)来改变旋转动作;里面的drawRect方法在类中。

显然,我不能嵌套两个定时器,因为“禁用”tapGesture,所以当我改变屏幕并返回时,旋转停止了,这有助于发现这种情况。

感谢您的帮助。

0

这可能是与上海华盈的框架问题。什么观点持ballView00?难道ballView完全位于内它是上海华盈的界限?

一般当你遇到问题时,你期望有一个触摸,但没有得到它,这是因为设置为接受触摸的视图超出了它父母的界限之一

所以,如果superview边界是100x100,并且球是在x = 125 y = 125时,它将不会收到触摸

当你的观点变得很好时, id out,因为一些不正确的约束,有时找到一个带有0x0维度的值并不奇怪。

您可以通过给他们一个背景颜色来轻松检查所有视图的边界。

+0

好主意@kball,让我检查,我稍后返回结果。 – Beto

+0

不幸的是仍然是同样的问题。我重新配置我的ImageView,但不起作用。实际上,我更新了我的UIImageView配置代码,并添加了黑色背景的球和UIView的图像。 – Beto

相关问题