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)")
}