我知道如何通过 IB 添加手势识别器,但我试图在不使用 IB 的情况下解决这个问题。
所以现在基本上是
blue1.userInteractionEnabled = YES;
UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePan:)];
[blue1 addGestureRecognizer:pgr];
[pgr release];
我的 handlePan 是
-(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
如果我在 IB 上做这件事,效果很好,我可以拍摄这件作品。
我不明白是什么导致它不像现在编码的那样移动。
感谢任何帮助。
我还在我的 handlePan 中尝试了 -(void) 而不是 -(IBAction),但这也没有用。
最佳答案
这在 IB 中有效,但在代码中无效,这表明错误出在第一段代码中。我怀疑 blue1
是 nil
。确保在 viewDidLoad
之前不要尝试修改 View 。它们在 initWithFrame:
和 awakeFromNib
中都将是 nil
。
关于iphone - 以编程方式添加手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9352224/