Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle selection of custom CallOutView swift

I've created a custom callOutView, which seem to work fine. However i have problem with handling tap on the custom callOut view. I would like push to a specific viewController when it is selected. How can i achieve this?

below i've added my FBSingleClusterView, which is the custom MKAnnotationView and the bubbleView which is the custom callOutView. beside this i ofcourse have an viewController with a 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)
    }



}
like image 401
Peter Pik Avatar asked Oct 29 '15 17:10

Peter Pik


1 Answers

Have you tried using instantiateViewControllerWithIdentifier? You can trigger this when tapping a button, in the viewDidLoad or in whatever other method you want it to trigger and push a new a viewController.

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

enter image description here

Example:

Here's an example of how you can push to a new viewController when tapping a cell. As mentioned you can use this method in different functions to push to 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 func.

I asked a similar question not too long ago. Here's the reference: Swift: Triggering TableViewCell to lead to a link in a UIWebView in another ViewController

Here's the link from Apple Dev: InstantiateViewControllerWithIdentifier

like image 142
Lukesivi Avatar answered Oct 20 '22 06:10

Lukesivi