Manual selection of custom CallOutView swift

I created a custom callOutView that seems to work fine. However, I have a problem handling touch on the user view of callOut. I would like to click on a specific viewController when it is selected. How can I achieve this?

below I added my FBSingleClusterView , which is a regular MKAnnotationView, and bubbleView , which is a regular callOutView. next to this I mean viewController with mapView.

FBSingleClusterView Variables

 private var hitOutside:Bool = true var preventDeselection:Bool { return !hitOutside } 

FBSingleClusterView Methods

 override func setSelected(selected: Bool, animated: Bool) { let calloutViewAdded = bubbleView?.superview != nil if (selected || !selected && hitOutside) { super.setSelected(selected, animated: animated) } self.superview?.bringSubviewToFront(self) if (bubbleView == nil) { bubbleView = BubbleView() } if (self.selected && !calloutViewAdded) { bubbleView?.clipsToBounds = true bubbleView?.layer.masksToBounds = true self.addSubview(bubbleView!) let discView = UIImageView() discView.contentMode = UIViewContentMode.Center discView.image = UIImage() discView.image = UIImage(named: "Disclosure") bubbleView?.contentView.addSubview(discView) let nameLabel = UILabel() nameLabel.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] nameLabel.font = UIFont.systemFontOfSize(10) nameLabel.textColor = UIColor.whiteColor() nameLabel.text = companyString?.uppercaseString bubbleView?.addSubview(nameLabel) discView.snp_makeConstraints { (make) -> Void in make.top.equalTo(bubbleView!).offset(0) make.height.equalTo(30) make.width.equalTo(20) make.right.equalTo(bubbleView!).offset(0) } nameLabel.snp_makeConstraints { (make) -> Void in make.top.equalTo(bubbleView!).offset(0) make.left.equalTo(bubbleView!).offset(10) make.height.equalTo(30) make.right.equalTo(bubbleView!).offset(-20) } let nameLabelWidth = nameLabel.requiredWidth(companyString!.uppercaseString, font: UIFont.systemFontOfSize(10)) + 35 let bubbleHeight = 35 as CGFloat bubbleView?.frame = CGRectMake((self.frame.width/2)-(nameLabelWidth/2), -bubbleHeight-2, nameLabelWidth, bubbleHeight) } if (!self.selected) { bubbleView?.removeFromSuperview() } } override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { var hitView = super.hitTest(point, withEvent: event) if let callout = bubbleView { if (hitView == nil && self.selected) { hitView = callout.hitTest(point, withEvent: event) } } hitOutside = hitView == nil return hitView; } 

bubbleView methods

  override public func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? { let viewPoint = superview?.convertPoint(point, toView: self) ?? point // let isInsideView = pointInside(viewPoint, withEvent: event) let view = super.hitTest(viewPoint, withEvent: event) return view } override public func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { return CGRectContainsPoint(bounds, point) } } 
+6
source share
2 answers

Have you tried using instantiateViewControllerWithIdentifier ? You can call this by clicking the button in viewDidLoad or any other way you want to call, and click on the new viewController.

First, set the "Storyboard ID" for the viewController in your storyboard. Like this:

enter image description here

Example:

Here is an example of how you can click on a new viewController when you click on a cell. As already mentioned, you can use this method in different functions to click on another viewController:

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let yourNewView = self.storyboard!.instantiateViewControllerWithIdentifier("yourStoryBoardID") as! ViewControllerYourePushingTo self.presentViewController(yourNewView, animated: true, completion: nil) } 

In your case, this is in the hitTest function.

I asked a similar question not so long ago. Here's the link: Swift: Running TableViewCell will result in a link in a UIWebView in another ViewController

Here's a link from Apple Dev: InstantiateViewControllerWithIdentifier

+3
source

You can add a gesture to a custom annotation view in didselectannotation and switch to another controllor view from the gesture action

+1
source

All Articles