ios7中的手势无法正常工作

时间:2014-02-24 09:56:20

标签: ios objective-c gesture

我使用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.h中,我制作:

   @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函数。

1 个答案:

答案 0 :(得分:4)

您在目标方法:中错过了(panGestureRecognizer:)。只需使用此..

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