简体   繁体   English

如何知道在Swift中单击哪个UIView

[英]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. 单击时有8个UIView,我想导航到另一个View Controller,但是当我单击8个UIView中的任何一个时,它目前仅指向一个ViewController,即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. 我已经将硬编码导航置于单击上,但是我希望每个UIView都应导致一个不同的ViewController,但我无法做到这一点。 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)". UIGestureRecognizer具有一个名为view的属性,从文档中可以看到“手势识别器附加到的视图。(只读)”。 So in your action method (looks like flipView ) you can call sender.view and then act on it. 因此,在您的操作方法(看起来像flipView )中,您可以调用sender.view ,然后对其进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何知道点击了哪个标签栏菜单 - How to know which Tabbar Menu Clicked 如何检测Swift 2中单击的表格单元格 - How to detect which table cell is clicked in swift 2 如何知道UIScrollView显示了哪个UIView索引或标签? - How to know which UIView index or tag has been shown by UIScrollView? ios如何从xib单击时知道哪个UIView? - ios How to know which UIView when I click from xib? Swift:如何知道哪个情节提要板处于活动状态 - Swift: How to know which Storyboard is active 如何知道选择哪个部分? 迅速 - How to know which section is selected ? Swift swift如何知道要导入哪些文件? - How does swift know which files to import? Swift:如何判断myView与哪些UIView相交? - Swift: How to tell which UIView(s) myView is intersecting with? 如何知道在调用activityController setCompletionHandler之前在UIActivityViewController中单击了哪个图标? - How to know which icon is clicked in UIActivityViewController before activityController setCompletionHandleris called? 如何知道在UICollectionView中单击了自定义单元格的哪一部分? - How to know which part of a customised cell have been clicked on in UICollectionView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM