2013-07-12 62 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

相關問題