簡體   English   中英

你如何在 storyboard 上制作一個 UIImageView 可點擊(快速)

[英]How do you make an UIImageView on the storyboard clickable (swift)

我是 swift(和一般的 Xcode 開發)的新手,我想知道如何使 storyboard 上的 ImageView 可點擊。 我要做的是讓它在單擊時顯示另一個視圖 controller。

您可以為此添加tapGesture。 這是代碼:

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()
    // create tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")

    // add it to the image view;
    imageView.addGestureRecognizer(tapGesture)
    // make sure imageView can be interacted with by user
    imageView.userInteractionEnabled = true
}

func imageTapped(gesture: UIGestureRecognizer) {
    // if the tapped view is a UIImageView then set it to imageview
    if let imageView = gesture.view as? UIImageView {
        println("Image Tapped")
        //Here you can initiate your new ViewController

        }
    }
}

Swift 3.0

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped(gesture:)))

        // add it to the image view;
        imageView.addGestureRecognizer(tapGesture)
        // make sure imageView can be interacted with by user
        imageView.isUserInteractionEnabled = true
    }

    func imageTapped(gesture: UIGestureRecognizer) {
        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            print("Image Tapped")
            //Here you can initiate your new ViewController

        }
    }
}

Swift 5.0

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // create tap gesture recognizer
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.imageTapped(gesture:)))

        // add it to the image view;
        imageView.addGestureRecognizer(tapGesture)
        // make sure imageView can be interacted with by user
        imageView.isUserInteractionEnabled = true
    }

    @objc func imageTapped(gesture: UIGestureRecognizer) {
        // if the tapped view is a UIImageView then set it to imageview
        if (gesture.view as? UIImageView) != nil {
            print("Image Tapped")
            //Here you can initiate your new ViewController

        }
    }
}

您可以更輕松地完成它,並通過Storyboard使圖像可以點擊,完全沒有編碼

  • 首先,您需要將UITapGestureRecognizer拖到Storyboard中的UIImageView上。
  • 然后使用@IBAction func imageClicked(_ sender: Any) {}創建要在代碼中運行的IBAction @IBAction func imageClicked(_ sender: Any) {}
  • 接下來,您需要通過在Document Outline選擇手勢識別器,然后切換到Connection Inspector Tab並將Sent Actions - > Selector拖到UIViewController ,然后選擇您創建的相應操作,將UITapGestureRecognizer連接到UITapGestureRecognizer中的IBAction 。先前。
  • 最后,您必須在圖像視圖上設置“ User Interaction Enabled ”復選框。

完成,一個完全可點擊的UIImageView,無需編寫一行代碼,除了您要調用的明顯函數。 但是,嘿,如果你想要推動一個segue,那么你可以通過使用手勢識別器Triggered Segues而不是它的Sent Actions來完成編碼。

盡管Storyboard有其局限性,但無需為可點擊圖像編寫代碼。 ;)

在此輸入圖像描述

我建議創建一個沒有文本的UIButton,並將其制作成您想要的圖像。 執行此操作后,您可以從圖像按CTRL拖動到要切換到的視圖控制器。 或者您可以在視圖控制器的代碼中進行IBAction手動分段。

對於swift 3.0版,請嘗試以下代碼

override func viewDidLoad() {
super.viewDidLoad()

let tap1 = UITapGestureRecognizer(target: self, action: #selector(tapGesture1))
imageview.addGestureRecognizer(tap1)
imageview.isUserInteractionEnabled = true
}
func tapGesture1() {
    print("Image Tapped")
}

在故事板上設置圖像視圖,啟用用戶交互,然后使用此方法

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];

    if ([touch view] == yourImageView)
    {
            //add your code for image touch here 
    }
}

首先,創建一個圖像視圖。 比寫這段代碼覆蓋 func viewDidLoad() { super.viewDidLoad()

let tap = UITapGestureRecognizer(target: self, action:          #selector(tapGesture))
imageview.addGestureRecognizer(tap)
imageview.isUserInteractionEnabled = true
}
func tapGesture() {
    print("Image View Tapped")
}

在您看來 controller。

UITapGestureRecognizer 方法做的一切都是可點擊的

我通過設置用戶交互enable = true來實現這一點

以下代碼在TouchesBegan中......干凈簡單

ImageView也在StackView中

if let touch = touches.first {
   if touch.view == profilePicture {
      print("image touched")
   }
}

好吧,雖然你可以使用上面提到的UITapGestureRecognizer,如果你需要做一個快速的項目,只關心應用程序的功能,最簡單的方法是有一個透明的按鈕覆蓋你的UIImageView沒有文字,然后使用寫下你想要的任何代碼。 當然,如果您正在制作一個非常重要的項目或其他東西,但是如果您只想制作一個快速的應用程序(假設某個會議的MVP非常簡單),這個方法應該可以正常工作。

在我看來,合理的方法是創建 UIImage 的擴展......

這是我找到的代碼...

public typealias SimpleClosure = (() -> ())
private var tappableKey : UInt8 = 0 
private var actionKey : UInt8 = 1 

extension UIImageView {
    
    @objc var callback: SimpleClosure {
        get {
            return objc_getAssociatedObject(self, &actionKey) as! SimpleClosure
        }
        set {
            objc_setAssociatedObject(self, &actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    var gesture: UITapGestureRecognizer {
        get {
            return UITapGestureRecognizer(target: self, action: #selector(tapped))
        }
    }
    
    var tappable: Bool! {
        get {
            return objc_getAssociatedObject(self, &tappableKey) as? Bool
        }
        set(newValue) {
            objc_setAssociatedObject(self, &tappableKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
            self.addTapGesture()
        }
    }

    fileprivate func addTapGesture() {
        if (self.tappable) {
            self.gesture.numberOfTapsRequired = 1
            self.isUserInteractionEnabled = true
            self.addGestureRecognizer(gesture)
        }
    }

    @objc private func tapped() {
        callback()
    }
}

用法應該是這樣的

    @IBOutlet weak var exampleImageView: UIImageView! {
    didSet {
        self.exampleImageView.tappable = true
    }
}override func viewDidLoad() {
    super.viewDidLoad()
    self.exampleImageView.callback = {
         //TODO: Here you put the On click code.
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM