2014-02-24 67 views
0

我使用UIView製作一個自定義視圖名稱「Cus_TextEdit」。 文件Cus_TextEdit.mios7中的手勢不起作用

@implementation Cus_TextView 
    @synthesize text; 
    - (id) initWithFrame:(CGRect)frame textNew:(NSString*)textNew { 
      self = [super initWithFrame:frame]; 
      if (self) { 
       [self setText:textNew]; 
       UIColor* bg = [UIColor colorWithRed:225 green:0 blue:0 alpha:1]; 
       self.backgroundColor = bg; 
      } 
     return self; 

     } 
    - (void)drawRect:(CGRect)rect { 
     self.text drawAtPoint:CGPointMake(5, 0) withFont:[myFont]; 
    } 

在文件ViewControler.hi使:

@property (retain, nonatomic) UIPanGestureRecognizer* panGestureRecognizer; 

    - (IBAction)clickBtn:(id)sender; 

當我點擊主視圖按鈕,功能clickBtn將調用代碼(在文件ViewController.m):

Cus_TextView* cus_tv = [[Cus_TextView alloc] initWithFrame: CGRectMake(10, 10, 100, 50) textNew:@"Hello"]; 
     [cus_tv setUserInteractionEnabled:YES]; 
     UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer)]; 
     [cus_tv addGestureRecognizer:panGesture]; 
     [self.view addSubview:cus_tv]; 

實現功能panGestureRecognizer

 -(void) panGestureRecognizer:(UIPanGestureRecognizer*) gesture { 
      NSLog(@"panGestureRecognizer called"); 
      CGPoint newPoint = [gesture locationInView:[self view]]; 
     [[gesture view] setCenter:newPoint]; 
     } 

當我運行應用程序,單擊按鈕,顯示文本「你好」。但是當我點擊它時,panGestureRecognizer函數不會被調用。

回答

4

您錯過了:目標方法(panGestureRecognizer:)。只是用這個..

UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];