2012-08-03 68 views
2

我知道這個問題似乎已經被問及答案,但我無法解決我的問題。TouchesMoved UIButton xcode 4.4 iOS 5.1.1

我想將我的觀點的對象跟隨手指的水平幻燈片(去上一頁或下一頁)

對於這一點,我使用的touchesBegan,TouchesMoved和TouchesEnded方法。

它在觸摸開始於視圖背景時正常工作,但在uibuttons觸摸開始時不起作用。

爲了解決這個問題,我已經子類,我想允許手勢的uibuttons,它工作正常,當我在Xcode的3.x和4.x版的iOS我剛裝了OSX頗富獅子

今天並開始使用xCode 4.4和iOS 5.1.1(iPAD)。 在這裏,我與同樣的應用程序,工作得很好,不再工作。

這裏是我的UIButton子類:

// myUIButton.h 

#import <UIKit/UIKit.h> 

@interface myUIButton : UIButton 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

@end 


// myUIButton.m 

#import "myUIButton.h" 

@implementation myUIButton 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"uibutton touches began"); 
    [self.nextResponder touchesBegan:touches withEvent:event]; 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"uibutton touches moved"); 
    [self.nextResponder touchesMoved:touches withEvent:event]; 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"uibutton touches ended"); 
    [self.nextResponder touchesEnded:touches withEvent:event]; 
} 

@end 

以下是我的主類中的方法:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"view touches began"); 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"view touches moved"); 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"view touches ended"); 
} 

當我觸摸開始上的UIButton我從子類,但主要得到各的NSLog touchesMoved只執行一次。 (self.nextResponder似乎只能運行一次)

是否有任何更改,使其無法在xcode 4.4或iOS 5.1.1?

當我第一次嘗試這樣做(在Xcode的3.x和4.x版的iOS),我發現這裏的解決方案: Is there a way to pass touches through on the iPhone?

但它不工作了...

你能幫我?

非常感謝。

編輯:問題是使用相同的[超級倒是...]代替或更多的[self.nextResponder倒是...]

+0

你解決了嗎?我面臨着類似的問題。我有Fingerpaint,它可以在iOS 6上正常工作,但在我的新iPad iOS7上,它不會呈現,直到touchesEnded。 touchesBegan之後只繪製了一小部分。 – 2013-11-16 21:00:15

+0

很抱歉回答遲到,但郵件已經發送到垃圾郵件箱。 我確實解決了我的問題,但不記得我是如何編寫我認爲正確的答案的。 – 2014-01-16 15:18:40

回答

0

前一段時間我的問題解決了,不記得如何但是我的文件存在差異,所以我會在這裏寫出適用的新版本。

// myUIButton.h 
#import <Foundation/Foundation.h> 

@interface myUIButton : UIButton { 
} 

@end 

// myUIButton.m 

#import "myUIButton.h" 

@implementation myUIButton 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"uibutton touches began"); 
    [super touchesBegan:touches withEvent:event]; 
    [self.nextResponder touchesBegan:touches withEvent:event]; 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"uibutton touches moved"); 
    [super touchesMoved:touches withEvent:event]; 
    [self.nextResponder touchesMoved:touches withEvent:event]; 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"uibutton touches ended"); 
    [super touchesEnded:touches withEvent:event]; 
    [self.nextResponder touchesEnded:touches withEvent:event]; 
} 

@end 

在每個視圖中,我需要這種行爲,我將在file.xib全景手勢識別器,我鏈接到行動panRecognizer:在file.m

// file.m 

@synthesize slide_origin_x; 

-(IBAction)panRecognizer:(UIPanGestureRecognizer *)recognizer { 
    // Recording movement 
    if (recognizer.state == UIGestureRecognizerStateBegan) { 
     self.slide_origin_x = [NSNumber numberWithFloat:recognizer.view.center.x]; 
    } 

    // Choose an action 
    else if (recognizer.state == UIGestureRecognizerStateEnded) { 
     int to_launch_action = 100; // Action choosen after 100 pixels 
     int touch_move = [self.slide_origin_x intValue] - recognizer.view.center.x; 
     if (touch_move > (0 + to_launch_action)) { 
      /* 
      * What happens if the view slide to the left (eg : go_to_next) 
      */ 
     } else if (touch_move < (0 - to_launch_action)) { 
      /* 
      * What happens if the view slide to the right (eg : go_to_previous) 
      */ 
     } 
     // The view goes back to normal 
     recognizer.view.center = CGPointMake([self.slide_origin_x intValue], recognizer.view.center.y); 
    } 

    // The view is moved 
    else { 
     CGPoint translation = [recognizer translationInView:self.view]; 
     recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
              recognizer.view.center.y); 
     [recognizer setTranslation:CGPointMake(0,0) inView:self.view]; 
    } 
} 

FYI定義:這作品也在XCode5和SDK6.1上