//
// BaseControl.h
// BaseButton
//
// Created by YouXianMing on 15/8/27.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <UIKit/UIKit.h>
@class BaseControl;
@protocol BaseControlDelegate <NSObject>
@optional
/**
* 点击事件触发
*
* @param control BaseControl对象
*/
- (void)baseControlTouchEvent:(BaseControl *)control;
@end
@interface BaseControl : UIView
/**
* 代理方法
*/
@property (nonatomic, weak) id <BaseControlDelegate> delegate;
#pragma mark - 以下方法需要子类重载
/**
* 触发了点击事件
*/
- (void)touchEvent;
/**
* 拖拽到rect外面触发的事件
*/
- (void)touchDragExit;
/**
* 点击事件开始
*/
- (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 {
if (self = [super initWithFrame:frame]) {
[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 | UIControlEventTouchCancel];
// 触发事件
[_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
//
// CustomButton.h
// CustomButton
//
// Created by YouXianMing on 16/5/21.
// Copyright © 2016年 YouXianMing. All rights reserved.
//
#import "BaseControl.h"
typedef NS_OPTIONS(NSUInteger, BaseControlState) {
BaseControlStateNormal = 1000,
BaseControlStateHighlighted,
BaseControlStateDisabled,
};
@interface CustomButton : BaseControl
/**
* 目标
*/
@property (nonatomic, weak) id target;
/**
* 按钮事件
*/
@property (nonatomic) SEL buttonEvent;
/**
* 普通背景色
*/
@property (nonatomic, strong) UIColor *normalBackgroundColor;
/**
* 高亮状态背景色
*/
@property (nonatomic, strong) UIColor *highlightBackgroundColor;