你将如何允许UIGestureRecognizer
的UIView
接收触控事件,但也确保其他,垫层/叠加UIView
也收到同样的触摸事件?
让我们说我有以下观点 - hierachie:
视图A(蓝色)和B(红色)都是同一超视图(灰色)的子视图.换句话说,他们都是兄弟姐妹,顺序决定了哪一个涵盖了另一个.
选项1:视图B(红色)具有常规UIButton
作为其子视图.选项2:视图A(蓝色)具有常规UIButton
作为其子视图.
给出选项1以便解释:视图A(蓝色UITapGestureRecognizer
)附加了(或者甚至更多,其他UIGestureRecognizers).什么是最优雅的方法,以确保它UIButton
确实接收其区域的所有接触,但视图A(蓝色)接收它的那些接触UITapGestureRecognizer
?
如果可能的话,解决方案不应该涉及扩展UIButton
-class并手动转发按钮本身的任何事件.想象一下,视图B将包含更多控件,而不仅仅是一个按钮.所有这些控件都应该以某种方式允许视图A接收它的触摸UIGestureRecognizer
.
我确实提供了两个选项,因为我不关心那些视图(A或B)中的哪一个是第一个,只要两个接收到触摸.
到目前为止,我的所有方法都突然达到了死胡同.也许自定义响应链是一个很好的解决方案?我一直认为在整个UIKit
/ UIEvent
/ UIResponder
堆栈中必须有一个优雅的解决方案.
我正在使用AVAudioPlayer的一个实例来播放音频文件.该应用程序配置为在后台播放音频,并设置适当的音频会话.我也成功接收了遥控事件.
这是代码:
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (nonatomic) AVAudioPlayer *player;
@end
@implementation ViewController
@synthesize player;
- (BOOL)canBecomeFirstResponder { return YES; }
- (void)viewDidLoad
{
[super viewDidLoad];
// Turn on remote control event delivery
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
// Set ourselves as the first responder
[self becomeFirstResponder];
// Set the audio session
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
// Play an …
Run Code Online (Sandbox Code Playgroud) 我有一个子类UIView
上的顶部UITableView
.我正在使用它UITableView
来显示一些数据,同时,我想覆盖手指后面的动画(例如,留下一条线索).
如果我是正确的,我需要的触摸事件要处理两个由UIView
子和UITableView
.我怎样才能做到这一点?是否可以touchesMoved
在UIView子类上触发,然后再触发UITableView
?
非常感谢你的帮助.
我找不到任何解释丢失的UITouch事件的东西.如果你在屏幕上粉碎你的整个手足够多次,touchesBegan的数量将不同于touchesEnded的数量!我认为实际了解这些孤立触摸的唯一方法是自己引用它们并跟踪它们没有移动多长时间.
示例代码:
int touchesStarted = 0;
int touchesFinished = 0;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
touchesStarted += touches.count;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
touchesFinished += touches.count;
NSLog(@"%d / %d", touchesStarted, touchesFinished);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud) 有没有办法获得触发UIEvent的触摸手势?所以,假设我用hitTest拦截了一个手势:withEvent:.如何识别触发此方法的触摸(捏,敲击,滑动等)?
我可以从UIEvent子类型获得远程事件的类型,即使设备被动摇,但触摸事件没有参数......
你如何识别这些?
我要求在前台处理耳机播放/暂停按钮事件.我怎么能使用下面的代码在后台处理相同的场景
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[self becomeFirstResponder];
NSLog(@"Responds!");
}
Run Code Online (Sandbox Code Playgroud)
如果可能,请帮助解释或示例代码.我做了很多研究但没有帮助.
在Mac上,人们可以随时mouseUp:
通过调用获取鼠标"在事件流之外"的位置(即,即使您没有为其他人订阅任何委托方法)[NSEvent mouseLocation]
.
iOS上有什么方法可以在不收听的情况下获取当前的触摸事件touchesBegan:
吗?
我问,因为至少有一种情况是没有调用touchesBegan:在app启动时,如果触摸已经在进行中,则touchesBegan永远不会被调用.实际上,也没有任何触摸相关的UIEvent方法被称为尽可能接近(sendEvent:
显然也不是UIApplication的/ UIWindow ).
我想根据启动时是否正在进行触摸来稍微改变我的应用程序的行为. 有没有办法检测应用程序启动时正在进行的触摸?
当我在选择几个项目后旋转应用程序两次时,它会崩溃.我已经覆盖了sendEvent方法,这是调试器停止的地方.当我尝试打印事件类型时,它会向我显示一些奇怪的东西(我认为它是一个不存在的内存位置):
(lldb) print event.type
(UIEventType) $R10 = <invalid> (0xff)
Run Code Online (Sandbox Code Playgroud)
不知何故,我认为这与我如何处理轮换有关.我有一个主 - 细节风格的应用程序,它使用不同类型的导航来垫景观,垫纵向和电话.我创建了一个名为的类NavigationFlowController
,它处理所有导航事件并相应地设置视图.在旋转时,它会分解视图树并使用正确的导航重新组合它们
func changeViewHierarchyForDevideAndOrientation(newOrientation:UIInterfaceOrientation? = nil){
print("MA - Calling master layout method")
UIApplication.myDelegate().window?.frame = UIScreen.mainScreen().bounds
let idiom = UIDevice.currentDevice().userInterfaceIdiom
var orientation:UIInterfaceOrientation!
if let no = newOrientation{
orientation = no
}else{
orientation = UIApplication.sharedApplication().statusBarOrientation
}
print("MA - Breaking up view tree...")
breakupFormerViewTree([sidebarViewController, listViewController, detailViewController, loginViewController])
print("MA - Start init navbackbone")
initNavBackboneControllers()
guard let _ = UIApplication.myDelegate().currentUser else {
if idiom == UIUserInterfaceIdiom.Phone{
currentState = AppState.PHONE
}else if idiom == …
Run Code Online (Sandbox Code Playgroud) 我有3个UIViews堆叠在另一个上面
UITableview
planeView
rootView
TableView位于顶部,rootView位于底部.(因为TableView位于其上,因此rootView不可见)
我在rootView中实现了以下代码
/*code in rootView*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
Run Code Online (Sandbox Code Playgroud)
期望在触摸或移动最顶层视图(即TableView)时调用这些函数,但相反没有调用任何函数.
我还尝试在TableView中放入以下代码,以便调用rootView方法
/*code in TableView so that the rootView methods are called(TableView is the subview of rootView)*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)
正如所料,它确实如此,但问题是TableView代表喜欢
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
不叫.
是否有任何方法可以确保在TableView类(didSelectRow :)中实现的TableView委托和rootView中的touchesBegan:,touchesMoved ..函数也相应地调用?
即,当我点击TableCell中的两个(didSelectRow:atIndex)函数 - > TableView和(touchesBegan和touchesEnd)方法 - > rootView被调用.
最新更新:我创建了一个全新的项目,其构建方式与下面完全相同.然而,这个测试项目实际上工作得很好,即使它看起来是一样的...我注意到的一件事是破坏版本中的父视图控制器在-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
点击子视图控制器的视图时接收...在工作版本中,子视图控制器接收事件应该......就好像父视图阻止子视图或其他东西..?
进度更新:在拧了一下后,我发现直接将帧分配给子视图控制器时它可以正常工作.但是,我必须使用autolayout创建我的项目,这就是我所做的.出于某种原因,autolayout导致子视图控制器的视图无响应...
目前我有一个只有1个childviewcontroller的viewcontroller.childviewcontroller的视图显示得很好但我根本无法与childViewController进行交互.childViewController有几个UIButtons,但它们不响应点击.
我尝试在父视图控制器和子视图控制器中进行测试.
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touched!");
}
Run Code Online (Sandbox Code Playgroud)
单击子视图控制器视图的框架时,此方法在父视图控制器中响应,但不在子视图控制器中响应.
子视图控制器在直接创建它并将其设置为应用程序委托中的根视图控制器时也可以正常工作.
我在父视图控制器中使用的代码:
(void) viewDidLoad
{
[super viewDidLoad];
self.detailPageVC = [[GoalDetailsPageViewController alloc] init];
[self.view addSubview:self.detailPageVC.view];
[self addChildViewController:self.detailPageVC];
[self.detailPageVC didMoveToParentViewController:self];
}
Run Code Online (Sandbox Code Playgroud)
感谢您帮助新手!:)
uievent ×10
ios ×8
iphone ×2
objective-c ×2
uiresponder ×2
uiview ×2
audio ×1
cocoa-touch ×1
hittest ×1
ios6.1 ×1
iphone-5 ×1
multi-touch ×1
swift ×1
touch ×1
touch-event ×1
touchesmoved ×1
uitableview ×1
uitouch ×1