[英]Xcode: Changing Button Colour
目前,我正在Xcode 5中創建一個單一視圖應用程序,但是我陷入了困境。 有誰知道如何使按鈕根據用戶的操作改變顏色。 例如,我有一個按鈕,然后選擇要以哪種方式滑動用戶想要更改按鈕的顏色。 因此,如果按鈕向左滑動並且用戶向左滑動,它將變為綠色;如果用戶向右滑動,則按鈕將變為紅色。 有人可以幫忙嗎? 任何幫助將不勝感激。
:)
您可以使用setBackgroundColor。 例:
[yourButton setBackgroundColor:[UIColor redColor]];
在標題處創建方法
-(void)handleSwipeButton : (UISwipeGestureRecognizer*) sw;
將下面的代碼添加到viewDidload
UISwipeGestureRecognizer *swLeft = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeButton:)] autorelease];
swLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[yourButton addGestureRecognizer:swLeft];
UISwipeGestureRecognizer *swRight = [[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeButton:)] autorelease];
swRight.direction = UISwipeGestureRecognizerDirectionRight;
[yourButton addGestureRecognizer:swRight];
並添加到.m文件
-(void)handleSwipeButton : (UISwipeGestureRecognizer*) sw {
if (sw.direction == UISwipeGestureRecognizerDirectionLeft)
[yourButton setBackgroundColor:[UIColor greenColor]];
else
[yourButton setBackgroundColor:[UIColor redColor]];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.