自定义UIButton

本文详细介绍了UIButton的两种创建方式,包括+方法和-方法,并探讨了使用alloc创建时如何自定义按钮效果。此外,还讲解了UIButton的内容边距、图像与标题设置、系统方法,以及第一响应者链和UIControl事件处理机制。提供了相关链接以深入理解UIButton的工作原理。
摘要由CSDN通过智能技术生成

UIbutton有两种不同的创建方式:

1:+方法创建

通过 [UIButton buttonWithType:UIButtonTypeCustom] 创建的按钮不能手动release,交给系统好了。不能写 [button release];
除了常见风格外,我们还可以使用一些系统私有风格。

点击如下地址了解:

http://www.th7.cn/Program/IOS/2012/01/03/53700.shtml

使用加方法创建的button,生命周期交给系统管理。


2:-方法创建

使用alloc创建方法一般用于自定义uibutton时,因为使用alloc创建的button在按钮点击的时刻都没有高亮效果的。

但是可以通过setimage方法,设置按钮在不同状态下的图片效果。(或者也可以设置setbackgroundimage,如果需要同时设置settitle和setimage的话,title会被image覆盖掉。)


button系统方法详解:

    UIImageView           *_backgroundView;

    UIImageView           *_imageView;

    UILabel               *_titleView;

三个涉及到显示的属性,从名字就可以看出来的什么东西了吧。

@property(nonatomic)          UIEdgeInsets contentEdgeInsets;             // default is UIEdgeInsetsZero

@property(nonatomic)          UIEdgeInsets titleEdgeInsets;               // default is UIEdgeInsetsZero

这个类好像没有见过,经过百度知道这东西是用来确定button中image和title的相对位置的。

详见:

http://blog.csdn.net/yanxiaoqing/article/details/7230660

http://www.cocoachina.com/macdev/tips/2012/0608/4338.html


UIEdgeInsetsZero

@property(nonatomic)         BOOL         adjustsImageWhenHighlighted;   // default is YES. if YES, image is drawn darker when highlighted(pressed)

@property(nonatomic)         BOOL         adjustsImageWhenDisabled;      // default is YES. if YES, image is drawn lighter when disabled

这两个方法用来设置当处于不同状态时,修改其上图片的亮度。(默认为YES,也就是说,当disable时会暗一些,可以设置成no使保持原有颜色)


@property(nonatomic,retain)   UIColor     *tintColor NS_AVAILABLE_IOS(5_0)UI_APPEARANCE_SELECTOR;// default is nil. only valid for some button types

设置tintcolor,不是所有类型button都能设置。


- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)stateNS_AVAILABLE_IOS(6_0);// default is nil. title is assumed to be single line

同样,也支持NSAttributedString 的显示


------------------

- (CGRect)backgroundRectForBounds:(CGRect)bounds;

- (CGRect)contentRectForBounds:(CGRect)bounds;

- (CGRect)titleRectForContentRect:(CGRect)contentRect;

- (CGRect)imageRectForContentRect:(CGRect)contentRect;


设置相对于坐标系的值,bounds代表本地坐标系,也就是说,以自己左上角为原点的位置。

contentRect则代表内容区域,标题相对于内容区域的位置。

------------------------------------

关于第一响应者链:

优质好文:

http://www.myexception.cn/operating-system/1407418.html

http://blog.csdn.net/chun799/article/details/8223612

nextresponder与superview还是有区别的:

例如:如果一个button没有接受事件,那么事件先被传递给他的视图控制器,然后才传递给他的父类。

此时的nextresponder就是一个viewcontroller,而不是它的superview。


关于UIbutton的父类UIControl:

UIControl派生自UIView,它是所有能够响应事件的类的根类。

- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;

- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event;

- (void)cancelTrackingWithEvent:(UIEvent *)event;   // event may be nil if cancelled for non-event reasons, e.g. removed from window

UIControl附带四个方法用来响应UITouch事件。(自定义组件时,可以覆写这些方法实现对事件的处理)


- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;


// remove the target/action for a set of events. pass in NULL for the action to remove all actions for that target

- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

- (NSArray *)actionsForTarget:(id)target forControlEvent:(UIControlEvents)controlEvent;    // single event. returns NSArray of NSString selector names. returns nil if none

// send the action. the first method is called for the event and is a point at which you can observe or override behavior. it is called repeately by the second.

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event;

- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents;                        // send all actions associated with events

这里可以实现时间的分发,主动分发事件给监听者。

UIEvent:

---------------------------------

- (NSSet *)allTouches;

- (NSSet *)touchesForWindow:(UIWindow *)window;

- (NSSet *)touchesForView:(UIView *)view;

- (NSSet *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture NS_AVAILABLE_IOS(3_2);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值