objective-c - UIPanRecognizer 不起作用。我的观点从未重绘

标签 objective-c ios xcode

我制作了一个像这样的 UIPanRecognizer。

- (void)pan:(UIPanGestureRecognizer *)gesture
{
    if ((gesture.state == UIGestureRecognizerStateChanged) ||
        (gesture.state == UIGestureRecognizerStateEnded)) {
        CGPoint translation = [gesture translationInView:self];
        self.origin = CGPointMake(
        self.origin.x+translation.x,self.origin.y+translation.y);
        [gesture setTranslation:CGPointZero inView:self];
}

但是我的 View 永远不会被重绘,这意味着......我的drawRect不会被再次调用。有人尝试过这个吗?

在我的 viewController 中我有这个

UIGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.graphView action:@selector(pan:)];
[self.graphView addGestureRecognizer:pan]; 
[self updateUI];

我的更新UI是

- (void)updateUI { [self.graphView setNeedsDisplay]; } 

最佳答案

当您将手势识别器附加到 View 时,您正在调用 updateUI 方法。您应该在回调方法 pan: 中执行此操作。

此外,您正在泄漏内存。将 UIGestureRecognizer 分配并添加到 View 后,您应该release 它。

关于objective-c - UIPanRecognizer 不起作用。我的观点从未重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7538799/

相关文章:

ios - 如何在 iOS 上调配类方法?

ios - 如何使用 Turbolinks-ios 打开链接的 pdf

ios - GDataXML 更新 xml 数据

ios - ld : framework not found FirebaseAnalytics after upgrading from 4. x.x 到 5.x.x firebase 通过 cocoapods

ios - 无法向 Swift 公开 Objective-C 类,进而导入 Swift 代码

ios - 以编程方式选择 TableView 单元格 : selectRowAtIndexPath vs setSelected

ios - UITableView 在从 connectionDidFinishLoading 获取数据之前出现

swift - Xcode:停止编译代码/函数行

c++ - 如何使用 Xcode 在 C++ 项目中添加静态库

ios - UITableViewController loadView 加载了 nib 但没有得到 UITableView。