简体   繁体   English

iOS7中的手势不起作用

[英]Gesture in ios7 not working

I make a custom view name "Cus_TextEdit" using UIView. 我使用UIView创建了一个自定义视图名称“ Cus_TextEdit”。 File Cus_TextEdit.m 文件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];
    }

In file ViewControler.hi make : 在文件ViewControler.hi中进行:

   @property (retain, nonatomic) UIPanGestureRecognizer* panGestureRecognizer;

   - (IBAction)clickBtn:(id)sender;

When i click a button on main view , function clickBtn will called with code(in file ViewController.m): 当我单击主视图上的按钮时,函数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];

Implement function panGestureRecognizer 实现功能panGestureRecognizer

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

When i run app,click button, text "Hello" displayed. 当我运行应用程序时,单击按钮,显示文本“ Hello”。 But when i click it, panGestureRecognizer function not be call. 但是当我单击它时,不会调用panGestureRecognizer函数。

You have missed : in target method (panGestureRecognizer:) . 您已经在目标方法(panGestureRecognizer:)错过了: Just Use this.. 只要使用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM