pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

允許 UIScrollView 及其子視圖都響應(yīng)觸摸

Allow UIScrollView and its subviews to both respond to a touch(允許 UIScrollView 及其子視圖都響應(yīng)觸摸)
本文介紹了允許 UIScrollView 及其子視圖都響應(yīng)觸摸的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我希望我的 UIScrollView 及其子視圖都能接收子視圖內(nèi)的所有觸摸事件.每個人都可以以自己的方式做出回應(yīng).

I want both my UIScrollView and its subviews to receive all touch events inside the subview. Each can respond in its own way.

或者,如果將點擊手勢轉(zhuǎn)發(fā)到子視圖,一切都會好起來的.

Alternatively, if tap gestures were forwarded to subviews, all would be well.

很多人都在這個一般領(lǐng)域苦苦掙扎.以下是眾多相關(guān)問題中的幾個:

A lot of people are struggling in this general area. Here are a few of the many related questions:

UIScrollView 如何從其子視圖中竊取觸摸
如何從 UIScrollView 竊取觸摸?
如何在 UIScrollView 中取消滾動

順便說一句,如果我在滾動視圖中覆蓋 hitTest:withEvent:,只要 userInteractionEnabled 為 YES,我就會看到觸摸.但這并不能真正解決我的問題,因為:

Incidentally, if I override hitTest:withEvent: in the scroll view, I do see the touches as long as userInteractionEnabled is YES. But that doesn't really solve my problem, because:

1) 到那時,我不知道是不是水龍頭.
2) 有時我需要將 userInteractionEnabled 設(shè)置為 NO.

1) At that point, I don't know if it's a tap or not.
2) Sometimes I need to set userInteractionEnabled to NO.

為了澄清,是的,我想將水龍頭與平底鍋區(qū)別對待.點擊應(yīng)該由子視圖處理.平底鍋可以通過滾動視圖以通常的方式處理.

To clarify, yes, I want to treat taps differently from pans. Taps should be handled by subviews. Pans can be handled by the scroll view in the usual way.

推薦答案

首先,免責(zé)聲明.如果在 UIScrollView 上將 userInteractionEnabled 設(shè)置為 NO,則不會將觸摸事件傳遞給子視圖.據(jù)我所知,除了一個例外:在 UIScrollView 的父視圖上攔截觸摸事件,并將這些事件專門傳遞給 UIScrollView.不過,老實說,我不知道你為什么要這樣做.如果你想禁用特定的 UIScrollView 功能(比如......好吧,滾動),你可以很容易地做到這一點,而無需禁用 UserInteraction.

First, a disclaimer. If you set userInteractionEnabled to NO on the UIScrollView, no touch events will be passed to the subviews. So far as I'm aware, there's no way around that with one exception: intercept touch events on the superview of the UIScrollView, and specifically pass those events to the subviews of UIScrollView. To be honest, though, I don't know why you would want to do this. If you're wanting to disable specific UIScrollView functionality (like...well, scrolling) you can do that easily enough without disabling UserInteraction.

如果我理解您的問題,您需要 UIScrollView 處理點擊事件 并傳遞給子視圖嗎?無論如何(無論手勢是什么),我認(rèn)為您正在尋找的是協(xié)議方法 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 在協(xié)議 UIGestureRecognizerDelegate 中.在您的子視圖中,無論您擁有什么手勢識別器,都在手勢識別器上設(shè)置一個委托(可能是首先設(shè)置 UIGestureReconginzer 的任何類).覆蓋上述方法并返回 YES.現(xiàn)在,該手勢將與任何其他可能竊取"該手勢的識別器一起被識別(在您的情況下,點擊).使用此方法,您甚至可以微調(diào)您的代碼以僅向子視圖發(fā)送某些類型的手勢或僅在某些情況下發(fā)送手勢.它給了你很多控制權(quán).請務(wù)必閱讀該方法,尤其是這一部分:

If I understand your question, you need tap events to be processed by the UIScrollView and passed to the subviews? In any case (whatever the gesture is), I think what you're looking for is the protocol method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: in the protocol UIGestureRecognizerDelegate. In your subviews, whatever gesture recognizers you have, set a delegate (probably whatever class is setting the UIGestureReconginzer in the first place) on the gesture recognizer. Override the above method and return YES. Now, this gesture will be recognized along with any other recognizers that might have 'stolen' the gesture (in your case, a tap). Using this method you can even fine tune your code to only send certain kinds of gestures to the subviews or send the gesture only in certain situations. It gives you a lot of control. Just be sure to read about the method, especially this part:

識別手勢時調(diào)用該方法手勢識別器或其他手勢識別器會阻止其他手勢識別器識別其手勢.注意返回 YES 保證允許同時識別;另一方面,返回 NO 并不能保證防止同時識別,因為其他手勢識別器的委托可能會返回 YES.

This method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer would block the other gesture recognizer from recognizing its gesture. Note that returning YES is guaranteed to allow simultaneous recognition; returning NO, on the other hand, is not guaranteed to prevent simultaneous recognition because the other gesture recognizer's delegate may return YES.

當(dāng)然,有一點需要注意:這只適用于手勢識別器.因此,如果您嘗試使用 touchesBegan:touchesEnded 等來處理觸摸,您可能仍然會遇到問題.當(dāng)然,您可以使用 hitTest: 將原始觸摸事件發(fā)送到子視圖,但為什么呢?當(dāng)您可以將 UIGestureRecognizer 附加到視圖并免費獲得所有這些功能時,為什么要使用 UIView 中的這些方法來處理事件?如果您需要以標(biāo)準(zhǔn) UIGestureRecognizer 無法提供的方式處理觸摸,subclass UIGestureRecognizer 并在那里處理觸摸.這樣您就可以獲得 UIGestureRecognizer 的所有功能以及您自己的自定義觸摸處理.我真的認(rèn)為 Apple 打算讓 UIGestureRecognizer 替換開發(fā)人員在 UIView 上使用的大部分(如果不是全部)自定義觸摸處理代碼.它允許代碼重用,并且在減輕哪些代碼處理哪些觸摸事件時更容易處理.

Of course, there's a caveat: This only applies to gesture recognizers. So you may still have problems if you're trying to use touchesBegan:, touchesEnded, etc to process the touches. You can, of course, use hitTest: to send raw touch events on to the subviews, but why? Why process the events using those methods in UIView, when you can attach a UIGestureRecognizer to a view and get all of that functionality for free? If you need touches processed in a way that no standard UIGestureRecognizer can provide, subclass UIGestureRecognizer and process the touches there. That way you get all the the functionality of a UIGestureRecognizer along with your own custom touch processing. I really think Apple intended for UIGestureRecognizer to replace most (if not all) of the custom touch processing code that developers use on UIView. It allows for code-reuse and it's a lot easier to deal with when mitigating what code processes what touch event.

這篇關(guān)于允許 UIScrollView 及其子視圖都響應(yīng)觸摸的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

iOS - Using storyboard and autolayout to center the UIScrollView(iOS - 使用故事板和自動布局使 UIScrollView 居中)
get index or tag value from imageview tap gesture(從 imageview 點擊手勢獲取索引或標(biāo)簽值)
UIScrollView not scrolling regardless of large contentSize(無論內(nèi)容大小如何,UIScrollView 都不會滾動)
Clean autorotation transitions in a paging UIScrollView(清除分頁 UIScrollView 中的自動旋轉(zhuǎn)轉(zhuǎn)換)
UIScrollView zooming with Auto Layout(UIScrollView 使用自動布局縮放)
How to create an image from a UIView / UIScrollView(如何從 UIView/UIScrollView 創(chuàng)建圖像)
主站蜘蛛池模板: 耐磨焊丝,堆焊焊丝,耐磨药芯焊丝,碳化钨焊丝-北京耐默公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 成都茶楼装修公司 - 会所设计/KTV装修 - 成都朗煜装饰公司 | 缠膜机|缠绕包装机|无纺布包装机-济南达伦特机械设备有限公司 | 智能案卷柜_卷宗柜_钥匙柜_文件流转柜_装备柜_浙江福源智能科技有限公司 | 成人纸尿裤,成人尿不湿,成人护理垫-山东康舜日用品有限公司 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 酒瓶_酒杯_玻璃瓶生产厂家_徐州明政玻璃制品有限公司 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 一氧化氮泄露报警器,二甲苯浓度超标报警器-郑州汇瑞埔电子技术有限公司 | 超声波气象站_防爆气象站_空气质量监测站_负氧离子检测仪-风途物联网 | 悬浮拼装地板_幼儿园_篮球场_悬浮拼接地板-山东悬浮拼装地板厂家 | 背压阀|减压器|不锈钢减压器|减压阀|卫生级背压阀|单向阀|背压阀厂家-上海沃原自控阀门有限公司 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 郑州律师咨询-郑州律师事务所_河南锦盾律师事务所 | 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 踏板力计,制动仪,非接触多功能速度仪,逆反射系数测试仪-创宇 | 齿轮减速马达一体式_蜗轮蜗杆减速机配电机-德国BOSERL齿轮减速电动机生产厂家 | 生产加气砖设备厂家很多,杜甫机械加气砖设备价格公道 | 铝箔-铝板-花纹铝板-铝型材-铝棒管-上海百亚金属材料有限公司 | 齿式联轴器-弹性联轴器-联轴器厂家-江苏诺兴传动联轴器制造有限公司 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 空压机商城|空气压缩机|空压机配件-压缩机网旗下商城 | 步进_伺服_行星减速机,微型直流电机,大功率直流电机-淄博冠意传动机械 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 万家财经_财经新闻_在线财经资讯网 | 科箭WMS仓库管理软件-TMS物流管理系统-科箭SaaS云服务 | 济南品牌设计-济南品牌策划-即合品牌策划设计-山东即合官网 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 深圳货架厂_仓库货架公司_重型仓储货架_线棒货架批发-深圳市诺普泰仓储设备有限公司 | 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 空气净化器租赁,空气净化器出租,全国直租_奥司汀净化器租赁 | 新疆散热器,新疆暖气片,新疆电锅炉,光耀暖通公司 | 防爆电机_ybx3系列电机_河南省南洋防爆电机有限公司 | 牛奶检测仪-乳成分分析仪-北京海谊| 雨水收集系统厂家-雨水收集利用-模块雨水收集池-徐州博智环保科技有限公司 | 全自动翻转振荡器-浸出式水平振荡器厂家-土壤干燥箱价格-常州普天仪器 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 桁架机器人_桁架机械手_上下料机械手_数控车床机械手-苏州清智科技装备制造有限公司 | 石油/泥浆/不锈钢防腐/砂泵/抽砂泵/砂砾泵/吸砂泵/压滤机泵 - 专业石油环保专用泵厂家 |