2011-01-07 131 views
4

我正在構建的應用程序有很多自定義UIButtons鋪設在相當精確的佈局圖像的頂部。按鈕,控制圖像和標籤,你有什麼,但有一個清晰的定製UIButton坐在上面來處理水龍頭。突出顯示一個自定義UIButton

我的客戶昨天說:「我希望當你點擊它時突出顯示」。沒關係,它立即推動一個新的uinavigationcontroller視圖...它不閃爍,所以他很困惑。 Oy公司。

這是我所做的解決它。我不喜歡它,但它是我做了什麼:

我的子類的UIButton(命名爲FlashingUIButton)。

出於某種原因,我不能在高亮顯示的控制模式下使用背景圖像進行配置。它似乎從未觸及過「突出顯示」的狀態。不知道這是爲什麼。

所以不是我寫的:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self setBackgroundImage:[UIImage imageNamed:@"grey_screen"] forState:UIControlStateNormal]; 
    [self performSelector:@selector(resetImage) withObject:nil afterDelay:0.2]; 
    [super touchesBegan:touches withEvent:event]; 
} 

-(void)resetImage 
{ 
    [self setBackgroundImage:nil forState:UIControlStateNormal]; 
} 

這愉快地奠定了我的grey_screen.png(30%不透明的黑盒子)在按鈕上時,它的挖掘,並用一秒鐘後高興emptyness 0.2替換它。

這是好的,但它意味着我必須要經過我的許多碎粒和UIButtons改變我的所有按鈕FlashingUIButtons。這不是世界的盡頭,但我真的很希望把它作爲一個UIButton類別來解決,並且一舉成名。

任何建議比這更好的方法嗎?

回答

4

可以攔截觸摸事件與手勢識別,然後以編程識別器添加到您的所有uibuttons。例如:

// 
// HighlighterGestureRecognizer.h 
// Copyright 2011 PathwaySP. All rights reserved. 
// 

#import <Foundation/Foundation.h> 


@interface HighlightGestureRecognizer : UIGestureRecognizer { 
    id *beganButton; 
} 

@property(nonatomic, assign) id *beganButton; 

@end 

和實現:

// 
// HighlightGestureRecognizer.m 
// Copyright 2011 PathwaySP. All rights reserved. 
// 

#import "HighlightGestureRecognizer.h" 


@implementation HighlightGestureRecognizer 

@synthesize beganButton; 

-(id) init{ 
    if (self = [super init]) 
    { 
     self.cancelsTouchesInView = NO; 
    } 
    return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.beganButton = [[[event allTouches] anyObject] view]; 
    if ([beganButton isKindOfClass: [UIButton class]]) { 
     [beganButton setBackgroundImage:[UIImage imageNamed:@"grey_screen"] forState:UIControlStateNormal]; 
     [self performSelector:@selector(resetImage) withObject:nil afterDelay:0.2]; 

    } 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
} 

- (void)reset 
{ 
} 

- (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event 
{ 
} 

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer 
{ 
    return NO; 
} 

- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer 
{ 
    return NO; 
} 

- (void)resetImage 
{ 
    [beganButton setBackgroundImage: nil forState:UIControlStateNormal]; 
} 

@end 

嗯。我沒有測試這個代碼,所以如果它不起作用,不要起訴我,只是評論,我們會解決。我只是寫得很快,以供參考。即使它不起作用,邏輯仍然應該是健全的。

編輯:

忘了補充,你的手勢識別器添加到您的按鈕的方法是,像這樣:

HighlighterGestureRecognizer * tapHighlighter = [[HighlighterGestureRecognizer alloc] init]; 

[myButton addGestureRecognizer:tapHighlighter]; 
[tapHighlighter release]; 

所以基本上你聲明它,對其進行初始化,並然後添加它。之後,您會想要釋放它,因爲addGestureRecognizer會保留它。

+0

您可能必須使用它的方法,喜歡了setBackgroundImage,僅僅是因爲在執行,我宣佈它作爲ID前beganButton之前把(UIButton的*)。我不確定如果沒有這樣做,它會起作用。另一種方法是將它聲明爲一個UIView而不是id,因爲你知道它至少總是一個UIView。無論哪種方式,你都可以玩弄它。 – Omar 2011-01-07 16:50:33

2

你的adjustsImageWhenHighlighted = YES設置在你的按鈕上嗎?默認值爲YES,但可能是您在xib中更改了它。它在屬性檢查器中的「突出調整圖像」複選框:

IB Highlight Button

0

我有一個UIButton(Swift 2)子類化後的同樣的問題。當用戶觸摸的UIButton,(即使對號「突出顯示的調整圖像」在故事板中被檢查)的子類「的drawRect」函數不被調用。這可能是由於它是一個自定義的UIButton。

無論如何,這是簡單的處理,通過覆蓋「的touchesBegan」和「touchesEnded」事件。可以做更多的事情來處理按鈕的所有狀態(如禁用)。但是這裏是代碼。

斯威夫特2:

import Foundation 
import UIKit 

@IBDesignable 

class ppButton: UIButton { 
    override func drawRect(rect: CGRect) { 
     if self.state != UIControlState.Highlighted { 
      //SKit is another class containing a function to draw the custom button 
      SKit.drawPpButton(0, width: rect.width-2, height: rect.height-2) 
     } else { 
      //SKit is another class containing a function to draw the custom button 
      SKit.drawPpButton(0, width: rect.width-20, height: rect.height-20) 
     } 
    } 
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     super.touchesBegan(touches, withEvent: event) 
     self.setNeedsDisplay() 
    } 
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     super.touchesEnded(touches, withEvent: event) 
     self.setNeedsDisplay() 
    } 
}