簡體   English   中英

iOS7中的手勢不起作用

[英]Gesture in ios7 not working

我使用UIView創建了一個自定義視圖名稱“ Cus_TextEdit”。 文件Cus_TextEdit.m

    @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];
      }

當我運行應用程序時,單擊按鈕,顯示文本“ Hello”。 但是當我單擊它時,不會調用panGestureRecognizer函數。

您已經在目標方法(panGestureRecognizer:)錯過了: 只要使用這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM