【问题标题】:How to know which UIView is clicked In Swift如何知道在 Swift 中点击了哪个 UIView
【发布时间】:2015-07-01 13:16:36
【问题描述】:

有 8 个 UIView,我想在单击时导航到另一个视图控制器,但当前当我单击 8 个 UIView 中的任何一个时,它仅指向一个视图控制器,即DetailedViewController。我已经将硬编码导航放在了点击上,但我希望每个 UIView 都应该指向不同的 ViewController 而我无法做到这一点。这是我的代码:-

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]))

}

}

这是输出。需要帮助!

【问题讨论】:

  • 你为什么用 UIView 而不是 UIbutton

标签: ios swift uigesturerecognizer touchesbegan


【解决方案1】:

UIGestureRecognizer 有一个名为 view 的属性,从文档中说“手势识别器附加到的视图。(只读)”。因此,在您的操作方法(看起来像 flipView)中,您可以调用 sender.view 然后对其进行操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多