Unified Contacts

30 August, 2014

Adding UITapGestureRecognizer to UIImageView

By default, UIImageView doesn't respond to any touch, such as UITabGestureRecognizer. However, you can enable it by either attributes inspector or code.

- Attributes Inspector
check the box "User Interaction Enabled" in the Interaction section


















- Code
self.imageView.userInteractionEnabled = true



Then, add a UITabGestureRecognizer to UIImageView.

var tapGesture = UITapGestureRecognizer(target: self, action: "tapped:")
self.imageView.addGestureRecognizer(tapGesture)

func tapped(recognizer: UITapGestureRecognizer) {
    println("tapped")
}

Okay, finished. Run and test it :)





UIImageView預設是不會對任何觸碰有反應, 例如UITabGestureRecognizer. 但可以透過attributes inspector或code去啟動.
- Attributes Inspector
在Interaction部份, 鈎選"User Interaction Enabled".


















- Code
self.imageView.userInteractionEnabled = true



之後, 把UITabGestureRecognizer加到UIImageView中.

var tapGesture = UITapGestureRecognizer(target: self, action: "tapped:")
self.imageView.addGestureRecognizer(tapGesture)

func tapped(recognizer: UITapGestureRecognizer) {
    println("tapped")
}

完成了 :)

26 August, 2014

Problem with UIImage(named:) initializer

When I tried to use UIImage(named:) in an init of an object, I had got the following error.

I fixed it by adding an initializer before it. I found that in UIImage Class Reference, init(name:) is classified as "Cached Image Loading Routines", and others are classified as "Initializing Images". Does it mean init(named:) doesn't perform initialization?

當我使用 UIImage(named:) 時, 我得到以下錯誤.

我把一個 initializer 放在前面就解決這個問題. 在 UIImage Class Reference, init(named:) 是被分類為"Cached Image Loading Routines", 而其他被分類為"Initializing Images". 意思是不是 init(named:) 不會進行初始?

19 August, 2014

Initializing Properties of Object

In Swift, you must initialize all properties of an object either in declaration or in initialization. Otherwise, Xcode will warn you that there is property haven't initialized yet.

If you want to initialize these properties in init(), you should put the super.init() at the end of init(). You will get an error if you put super.init() before all properties have been initialized.