2016-05-17 8 views
2

我目前使用的應用程序,我試圖複製應用程序中發生的操作。如何從這個應用程序複製UIButton選擇的設計和動作?

下面是一個例子:

enter image description here

正如你所看到的,當我的手指在選擇了「UIButton」(至少這是我認爲他們是),背景圖像被突出顯示。不釋放我的手指,滾動瀏覽不同的選項會突出顯示特定的UIButton

我相信每個人都是UIButton沒有文字標籤,他們剛剛爲每個UIButton添加了UILabel

我儘快做類似的事情:

enter image description here

不過,我想不通他們是如何設法讓每個選擇的背景高亮顯示。

它看起來像是UIControlEvents.TouchDownUIControlEvents.TouchUpInside的組合。

我已經編寫了類似的東西:

cameraButton.addTarget(self, action: #selector(AddClothesViewController.selectCameraOptions(_:)), forControlEvents: .TouchUpInside) 

cameraButton.addTarget(self, action: #selector(AddClothesViewController.selectCameraOptionsHighlighted(_:)), forControlEvents: .TouchDown) 

func selectCameraOptionsHighlighted(button: UIButton) 
{ 
    if button == cameraButton 
    { 
     print("GO HERE") 
     cameraButton.setImage(UIImage(named: "highlighted background"), forState: .Normal) 

    } 
} 

func selectCameraOptions(button: UIButton) 
{ 
    if button == cameraButton 
    { 
     // DO STUFF 
    } 
} 

但是這種方法是行不通的,因爲如果我執行.TouchDown,不知何故相機選項中消失。如果我只是添加目標.TouchUpInside,那顯然不起作用,因爲只有當我釋放手指時纔會突出顯示,到那時,它已經讓我看到不同的視角,即相機或照片庫。

如何實現這一目標的最佳方法是什麼?

謝謝。

+4

這是來自'UIAlertController'的'ActionSheet'風格Ref:http://stackoverflow.com/a/32798896/3118477 – sargeras

+2

對iPad使用UIActionSheet – 2016-05-17 06:49:32

+0

,不要忘記設置操作表的'popoverPresentationController's'sourceRect'& 'sourceView'否則app會崩潰 –

回答

0

你可以像所有人提到的一樣使用Actionsheet或alterViewController,但是如果你想讓一個按鈕像這樣工作,可以爲highlitedstate添加不同的UIColor。

一旦你觸摸按鈕,按鈕將改變其狀態爲突出顯示。所以你需要改變UIButton觸摸時的背景顏色。

參考this答案更改突出顯示的狀態顏色。

相關問題