2015/2/24

Swift - 解決在 UIImageView 放一個 UIButton 按了沒反應的問題

以下示範在UIImageView 內放入一個 UIButton 並且加入按鈕事件,按下按鈕後在console顯示 onButtonClick 表示已成功觸發按下事件。

import UIKit

class TestViewController: UIViewController {

    var imageView:UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    var button:UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    override func viewDidLoad() {
        super.viewDidLoad()
        imageView.backgroundColor = UIColor.greenColor()
        button.backgroundColor = UIColor.redColor()
        self.view.addSubview(imageView)
        imageView.addSubview(button)
        button.addTarget(self, action: "onButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        imageView.userInteractionEnabled = true
    }

    func onButtonClick(){
        println("onButtonClick")
    }
    
    func test(){
        var allView = UITest.AllUIView()
        for (key,ui) in allView {
            println("\(key).userInteractionEnabled = \(ui.userInteractionEnabled)")
        }
    }
}

其中關鍵點在於 UIImageView 的 userInteractionEnabled 值預設是 false!

imageView.userInteractionEnabled = true

根據UIView的繼承樹, 我把全部繼承 UIView 的 Class 都實體化之後印出他們的 userInteractionEnabled 值,發現除了 UIImageView 跟 UILabel 的 userInteractionEnabled 值是 false,其他 Class 的 userInteractionEnabled 值都是 true 。 以下程式碼貼到 swift playground 即可看見結果。

import UIKit
import MediaPlayer
import SceneKit
import SpriteKit
import WebKit
import iAd
import GLKit
import MapKit

class UITest: NSObject {
   
    class func AllUIView()->[String:UIView]{
        //import UIKit
        return [
            "UIView":UIView()
            ,"UIActionSheet":UIActionSheet()
            ,"UIActivityIndicatorView":UIActivityIndicatorView()
            ,"UIAlertView":UIAlertView()
            ,"UICollectionReusableView":UICollectionReusableView()
            ,   "UICollectionViewCell":UICollectionViewCell()
            ,"UIControl":UIControl()
            ,   "UIButton":UIButton()
            ,   "UIDatePicker":UIDatePicker()
            ,   "UIPageControl":UIPageControl()
            ,   "UIRefreshControl":UIRefreshControl()
            ,   "UISegmentedControl":UISegmentedControl()
            ,   "UISlider":UISlider()
            ,   "UIStepper":UIStepper()
            ,   "UISwitch":UISwitch()
            ,   "UITextField":UITextField()
            ,   "UIImageView":UIImageView()
            ,   "UIInputView":UIInputView()
            ,   "UILabel":UILabel()
            ,"UINavigationBar":UINavigationBar()
            ,"UIPickerView":UIPickerView()
            ,"UIPopoverBackgroundView":UIPopoverBackgroundView()
            ,"UIProgressView":UIProgressView()
            ,"UIScrollView":UIScrollView()
            ,   "UICollectionView":UICollectionView(frame: CGRect.zeroRect, collectionViewLayout: UICollectionViewLayout())
            ,"UISearchBar":UISearchBar()
            ,"UITabBar":UITabBar()
            ,"UITableViewCell":UITableViewCell()
            ,"UITableViewHeaderFooterView":UITableViewHeaderFooterView()
            ,"UIToolbar":UIToolbar()
            ,"UIVisualEffectView":UIVisualEffectView()
            ,"UIWebView":UIWebView()
            ,"UIWindow":UIWindow()
            
            //import WebKit
            ,"WKWebView":WKWebView()
            
            //import iAd
            ,"ADBannerView":ADBannerView()
            
            //import GLKit
            ,"GLKView":GLKView()
            
            //import MapKit
            ,"MKAnnotationView":MKAnnotationView()
            ,   "MKPinAnnotationView":MKPinAnnotationView()
            ,"MKMapView":MKMapView()
            ,"MKOverlayView":MKOverlayView()
            ,   "MKOverlayPathView":MKOverlayPathView()
            
            //import MediaPlayer
            ,"MPVolumeView":MPVolumeView()
            
            //import SceneKit
            ,"SCNView":SCNView()
            
            //import SpriteKit
            ,"SKView":SKView()
        ]
    }
}

var allView = UITest.AllUIView()
for (key,ui) in allView {
    println("\(key).userInteractionEnabled = \(ui.userInteractionEnabled)")
}

沒有留言: