2011-01-07 104 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() 
    } 
}