简体   繁体   中英

How to know which UIView is clicked In Swift

There are 8 UIViews which I want to navigate to the other View Controller when clicked but currently when I clicked on any of the 8 UIView It points to only one view controller that is DetailedViewController. I had put the hard coded navigation on click but I want that every UIView should lead to a different ViewController And I am Unable to do that. Here is my code:-

class HomeViewController: UIViewController {
var view1:UIView!
var view2:UIView!
var view3:UIView!
var view4:UIView!
var view5:UIView!
var view6:UIView!
var view7:UIView!
var view8:UIView!



override func viewDidLoad() {

    super.viewDidLoad();

    view1 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xf6b37f, alpha: 1.0), title:"Kerala", logoImage:UIImage(named: "CD_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "kerala")!), backViewTitle:"Area:38,863 Km \n Capital: Thiruvananthapuram")
    view2 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xf9a451, alpha: 1.0), title:"Goa", logoImage:UIImage(named: "coffee_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "goa")!), backViewTitle:"Area:38,863 Km \n Capital: Panaji")
    view3 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xf78d60, alpha: 1.0), title:"Delhi", logoImage:UIImage(named: "message_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "delhi")!), backViewTitle:"Area:38,863 Km \n Capital: Delhi")
    view4 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xfc7d38, alpha: 1.0), title:"Gujarat", logoImage:UIImage(named: "things_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "gujarat")!), backViewTitle:"Area:38,863 Km \n Capital: Gandhinagar")
    view5 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xeb6100, alpha: 1.0), title:"Maharashtra", logoImage:UIImage(named: "clock_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "maharasathra")!), backViewTitle:"Area:38,863 Km \n Capital: Mumbai")
    view6 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xff5e33, alpha: 1.0), title:"Madhya Pradesh", logoImage:UIImage(named: "settings_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "bestplace")!), backViewTitle:"Area:38,863 Km \n Capital: Bhopal")
    view7 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xeb6100, alpha: 1.0), title:"Maharashtra", logoImage:UIImage(named: "clock_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "maharasathra")!), backViewTitle:"Area:38,863 Km \n Capital: Mumbai")
    view8 = self.autoLayoutPanelViewWithColor(UIColor(hex: 0xff5e33, alpha: 1.0), title:"Madhya Pradesh", logoImage:UIImage(named: "settings_grid@156px")!, backViewBackgroundColor:UIColor(patternImage: UIImage(named: "bestplace")!), backViewTitle:"Area:38,863 Km \n Capital: Bhopal")

    self.makeGlobalConstraintsToView(self.view, subviewArray: [view1,view2,view3,view4,view5,view6,view7,view8])

}


func autoLayoutPanelViewWithColor(bgColor:UIColor,title:String,logoImage:UIImage,backViewBackgroundColor:UIColor,backViewTitle:String) -> UIView {

    var view:PanelView           = PanelView(bgColor: bgColor)
    view.backViewTitle           = backViewTitle
    view.backViewBackgroundColor = backViewBackgroundColor
    self.imageViewWithImage(logoImage, title: title, inPanelView: view)
    self.addFlipTapGestureRecognizerToView(view)
    self.view.addSubview(view)

    return view

}

func addFlipTapGestureRecognizerToView(view:UIView) {

    var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "flipView:")
    tap.numberOfTapsRequired = 1

    view.addGestureRecognizer(tap)

}

func makeGlobalConstraintsToView(view:UIView,subviewArray:Array<AnyObject>) {

    var dic:NSDictionary = [
        "view1":subviewArray[0],
        "view2":subviewArray[1],
        "view3":subviewArray[2],
        "view4":subviewArray[3],
        "view5":subviewArray[4],
        "view6":subviewArray[5],
        "view7":subviewArray[6],
        "view8":subviewArray[7]
    ];
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view5][view6(==view5)]|",
        options: NSLayoutFormatOptions.AlignAllBaseline,
        metrics: nil,
        views: dic as [NSObject : AnyObject]))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view7][view8(==view7)]|",
        options: NSLayoutFormatOptions.AlignAllBaseline,
        metrics: nil,
        views: dic as [NSObject : AnyObject]))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view3][view4(==view3)]|",
        options: NSLayoutFormatOptions.AlignAllBaseline,
        metrics: nil,
        views: dic as [NSObject : AnyObject]))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view1][view2(==view1)]|",
        options: NSLayoutFormatOptions.AlignAllBaseline,
        metrics: nil,
        views: dic as [NSObject : AnyObject]))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-22-[view1][view3(==view1)][view5(==view1)][view7(==view1)]|",
        options: nil,
        metrics: nil,
        views: dic as [NSObject : AnyObject]))

    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-22-[view2][view4(==view2)][view6(==view2)][view8(==view2)]|",
        options: nil,
        metrics: nil,
        views: dic as [NSObject : AnyObject]))

}

func flipView(sender:UITapGestureRecognizer) {

    var flipDuration = 0.8
    UIView.transitionWithView(sender.view!,
        duration: flipDuration,
        options: UIViewAnimationOptions.TransitionFlipFromLeft,
        animations: {() -> Void in

        },
        completion: {(finished:Bool) -> Void in

            sleep(1)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let secondViewController = storyboard.instantiateViewControllerWithIdentifier("DetailedViewController") as! UIViewController
            self.presentViewController(secondViewController, animated: true, completion: nil)
        }
    )
    dispatch_after(dispatch_time_t(flipDuration / 2) * 1000, dispatch_get_main_queue(), { () -> Void in
        (sender.view as! PanelView).changeView()

    })

}

func imageViewWithImage(image:UIImage,title:String,inPanelView:UIView) {

    var panelLogo       = UIImageView(image: image)
    var label           = UILabel(frame: CGRectMake(0, 70, 130, 40))
    label.textColor     = UIColor.whiteColor();
    label.text          = title
    label.textAlignment = NSTextAlignment.Center
    label.lineBreakMode = .ByWordWrapping

    panelLogo.setTranslatesAutoresizingMaskIntoConstraints(false)
    panelLogo.addSubview(label)
    inPanelView.addSubview(panelLogo)
    inPanelView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[panelLogo]-20-|",
        options: NSLayoutFormatOptions.AlignAllBaseline,
        metrics: nil,
        views: ["panelLogo":panelLogo]))
    inPanelView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-25-[panelLogo]-25-|",
        options: NSLayoutFormatOptions.AlignAllBaseline,
        metrics: nil,
        views: ["panelLogo":panelLogo]))

}

}

This is the output. Need help!

在此处输入图片说明

UIGestureRecognizer has a property called view which, from the docs, says "The view the gesture recognizer is attached to. (read-only)". So in your action method (looks like flipView ) you can call sender.view and then act on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM