定制个性化按钮
效果
说明
通过捕捉一个按钮不同状态的值来定制我们自己的按钮动画,本人仅仅提供了实现的抽象基类以及一个简单的示例,剩下的需要你根据自己的想象力创造了.
源码
https://github.com/YouXianMing/UI-Component-Collection
//
// BaseControl.h
// BaseButton
//
// Created by YouXianMing on 15/8/27.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface BaseControl : UIView
/**
* ========================
* = override by subclass =
* ========================
*
* 触发点击事件
*/
- (void)touchEvent;
/**
* ========================
* = override by subclass =
* ========================
*
* 拖拽到rect外面触发的事件
*/
- (void)touchDragExit;
/**
* ========================
* = override by subclass =
* ========================
*
* 点击事件开始
*/
- (void)touchBegin;
@end
//
// BaseControl.m
// BaseButton
//
// Created by YouXianMing on 15/8/27.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import "BaseControl.h"
@interface BaseControl ()
@property (nonatomic, strong) UIButton *button;
@end
@implementation BaseControl
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self baseControlSetup];
}
return self;
}
- (void)baseControlSetup {
_button = [[UIButton alloc] initWithFrame:self.bounds];
[self addSubview:_button];
// 开始点击
[_button addTarget:self
action:@selector(touchBegin)
forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
// 拖拽到rect外面
[_button addTarget:self
action:@selector(touchDragExit)
forControlEvents:UIControlEventTouchDragExit];
// 触发事件
[_button addTarget:self
action:@selector(touchEvent)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)touchEvent {
[NSException raise:NSInternalInconsistencyException
format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
[NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}
- (void)touchDragExit {
[NSException raise:NSInternalInconsistencyException
format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
[NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}
- (void)touchBegin {
[NSException raise:NSInternalInconsistencyException
format:@"对不起,您不能直接调用 '%@ %d' 中的方法 '%@',您需要通过继承其子类,在子类中重载该方法",
[NSString stringWithUTF8String:__FILE__].lastPathComponent, __LINE__, NSStringFromSelector(_cmd)];
}
@end
细节