首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何像UITableView或ScollView一样处理触摸事件

如何像UITableView或ScollView一样处理触摸事件
EN

Stack Overflow用户
提问于 2012-05-27 20:55:43
回答 1查看 524关注 0票数 0

我做了一个自定义控件,继承自UIView,并在UIView上添加了很多UIButton。当用户触摸和移动时,我会做一些动画:让按钮通过函数touchesMoved移动

代码语言:javascript
代码运行次数:0
运行
复制
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

但buttonClick事件似乎具有更高的优先级。

我想它可以像UITableView一样,滚动的东西比按钮点击有更高的优先级。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-27 21:22:15

你需要调查一下UIPanGestureRecognizer

它允许您取消发送到其他处理程序的事件。

更新了有关如何保护以前的点的其他信息。

在操作回调中,您将得到初始触摸位置recognizer.state == UIGestureRecognizerStateBegan的通知。您可以将该点另存为实例变量。您还可以在不同的时间间隔内获得回调recognizer.state == UIGestureRecognizerStateChanged。您也可以保存此信息。然后,当您使用recognizer.state == UIGestureRecognizerStateEnded获得回调时,您将重置所有实例变量。

代码语言:javascript
代码运行次数:0
运行
复制
- (void)handler:(UIPanGestureRecognizer *)recognizer
{
    CGPoint location = [recognizer locationInView:self];
    switch (recognizer.state)
    {
        case UIGestureRecognizerStateBegan:
            self.initialLocation = location;
            self.lastLocation = location;
            break;
        case UIGestureRecognizerStateChanged:
            // Whatever work you need to do.
            // location is the current point.
            // self.lastLocation is the location from the previous call.
            // self.initialLocation is the location when the touch began.

            // NOTE: The last thing to do is set last location for the next time we're called.
            self.lastLocation = location;
            break;
    }
}

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10774129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档