将多个手势识别器分配给 AnyObject 时出错
Posted
技术标签:
【中文标题】将多个手势识别器分配给 AnyObject 时出错【英文标题】:Error while Assigning Multiple Gesture Recognizers to AnyObject 【发布时间】:2016-08-02 04:29:52 【问题描述】:大家好,我目前在这个错误上迷路了,这是我第一次使用 ios 和 Swift,虽然我已经能够使用 1 个手势识别器来查看视图,但我需要为任何对象设置多个手势识别器(component.control 有AnyObject 类型),当我运行它并按下按钮(作为 AnyObject 类型传递)时,我在 appDelegate.swift 文件中得到了这个错误
line 1: class AppDelegate: UIResponder, UIApplicationDelegate //Thread 1: EXC_BAD_ACCESS (code=1,address=0x1)
我们将不胜感激!谢谢!
这就是我所拥有的 覆盖 func viewDidLoad() super.viewDidLoad()
//gestures
let gesture_tap = UITapGestureRecognizer(target: self, action: #selector(ComponentDetailViewController.ctrl_tapped(_:event:)));
let gesture_pinch = UIPinchGestureRecognizer(target: self, action: #selector(ComponentDetailViewController.ctrl_pinched(_:event:)));
let gesture_swipe = UISwipeGestureRecognizer(target:self, action: #selector(ComponentDetailViewController.ctrl_swiped(_:event:)));
let gesture_longPress = UILongPressGestureRecognizer(target: self, action: #selector(ComponentDetailViewController.ctrl_longPressed(_:event:)));
let gesture_rotate = UIRotationGestureRecognizer(target:self,action: #selector(ComponentDetailViewController.ctrl_rotated(_:event:)));
let gesture_pan = UIPanGestureRecognizer(target:self, action: #selector(ComponentDetailViewController.ctrl_panned(_:event:)));
let ctrl = component?.control;
ctrl!.addGestureRecognizer(gesture_tap);
ctrl!.addGestureRecognizer(gesture_pinch);
ctrl!.addGestureRecognizer(gesture_swipe);
ctrl!.addGestureRecognizer(gesture_longPress);
ctrl!.addGestureRecognizer(gesture_rotate);
ctrl!.addGestureRecognizer(gesture_pan);
gesture_tap.delegate = self;
gesture_pinch.delegate = self;
gesture_swipe.delegate = self;
gesture_longPress.delegate = self;
gesture_rotate.delegate = self;
gesture_pan.delegate = self;
component?.control = ctrl as? UIView;
//component?.control!.userInteractionEnabled = true;
//component?.control!.addGestureRecognizer(tap);
viewDisplayComponent.addSubview((component?.control)! as! UIView);
//Gesture methods
func ctrl_tapped(ctrl: AnyObject, event:UIEvent)
setMessage("TO_tapped");
func ctrl_pinched(ctrl: AnyObject, event:UIEvent)
setMessage("TO_pinchedWithArgs")
func ctrl_swiped(ctrl: AnyObject, event:UIEvent)
setMessage("TO_swipedWithArgs");
func ctrl_longPressed(ctrl: AnyObject, event:UIEvent)
setMessage("TO_longPressedWithArgs");
func ctrl_rotated(ctrl: AnyObject, event:UIEvent)
//logTextView.text += "Rotated";
func ctrl_panned(ctrl: AnyObject, event:UIEvent)
setMessage("TO_pannedWithArgs");
【问题讨论】:
【参考方案1】:根据docs:
调用的动作方法必须符合以下之一 签名:
(void)handleGesture; (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer; 快速翻译: handleGesture(gestureRecognizer: UIGestureRecognizer)
然而,您所有的操作方法都有带有两个参数的签名。我不确定你是如何让一个手势识别器工作的。
我在 appDelegate.swift 文件中得到了这个错误:
第 1 行:类 AppDelegate:UIResponder、UIApplicationDelegate //线程1:EXC_BAD_ACCESS (code=1,address=0x1)
Xcode 调试区域的输出顶部,即 print() 语句的输出位置如何?
【讨论】:
以上是关于将多个手势识别器分配给 AnyObject 时出错的主要内容,如果未能解决你的问题,请参考以下文章
“无法将类型 'String' 的值分配给类型 'AnyObject?'”,Swift 3,Xcode 8 beta 6