目录
1. UIButton: UIControl 按钮
2. 手势
前言
注意:
UIImageView 和 UILabel 是少数几个不可交互的控件,需要设置userInteractionEnabled为true,否则不允许交互。
1. UIButton 按钮(: UIControl)
// 创建
UIButton *button=[UIButton new];
[self.view addSubview:button];
[button autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
常用
/*
UIControlStateNormal 普通状态
UIControlStateHighlighted 触摸状态(高亮)
UIControlStateDisabled 禁用状态
UIControlStateSelected 选中状态
UIControlStateFocused
UIControlStateApplication
UIControlStateReserved
*/
// 设置 标题
[button setTitle:@"" forState:UIControlStateNormal];
// 设置 标题颜色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
// 设置 字体
[button.titleLabel setFont:[UIFont systemFontOfSize:16]];
// 设置 标题阴影色
[button setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
// 设置 图片
// imageView的大小和UIbutton的大小是不一致的,iphoneX机型下很明显可以看到。
// 设置setContentVerticalAlignment、setContentHorizontalAlignment设置为fill可使大小一致
[button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
// 设置 图片显示模式
[button.imageView setContentMode:UIViewContentModeScaleToFill];
// 设置 背景图片
// 图片大小会和按钮大小保持一致
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
// 设置 背景颜色
[button setBackgroundColor:[UIColor redColor]];
// 设置 触摸时图片/背景图片是否变暗
[button setAdjustsImageWhenHighlighted:true];
// 设置 标题偏移
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// 设置 图片偏移
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// 设置 内容偏移
[button setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// 设置 内容纵向、水平对齐方式
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
/*
UIControlContentHorizontalAlignmentCenter = 0,
UIControlContentHorizontalAlignmentLeft = 1,
UIControlContentHorizontalAlignmentRight = 2,
UIControlContentHorizontalAlignmentFill = 3,
*/
/*
UIControlContentVerticalAlignmentCenter = 0, // 默认内容是居中的
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3, // 填充
*/
// 设置 是否选中(true则进入UIControlStateSelected状态)
[button setSelected:true];
// 设置 是否允许用户交互
[button setUserInteractionEnabled:true];
// 添加 点击事件
[button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
//-------------- 其他 --------------
// 设置 不可用时是否变暗
[button setAdjustsImageWhenDisabled:true];
// 设置 触摸时是否高亮
[button setShowsTouchWhenHighlighted:true];
// 获取 img、bgImg、titleColor、titleShadowColor、title、AttributedTitle
UIImage *img=button.currentImage;
UIImage *bgImg=button.currentBackgroundImage;
UIColor *titleColor=button.currentTitleColor;
UIColor *shadowTitleColor=button.currentTitleShadowColor;
NSString *title=button.currentTitle;
NSAttributedString *titleStr=button.currentAttributedTitle;
继承自UIView
// 设置 alpha透明度
[button setAlpha:0.5];
// 设置 是否隐藏
[button setHidden:true];
// 设置 填充色
[button setTintColor:[UIColor blueColor]];
// 设置是否可用
[button setEnabled:true];
// 设置 边框颜色、边框宽度、边框圆角、是否裁剪
[button.layer setBorderColor:[UIColor redColor].CGColor];
[button.layer setBorderWidth:1.0];
[button.layer setCornerRadius:5.0];
[button.layer setMasksToBounds:true];
系统自带UIButton类型
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
/*
UIButtonTypeCustom = 0, 前面不带图标, 默认文字颜色为白色,无触摸时高亮
UIButtonTypeSystem 前面不带图标, 默认文字颜色为蓝色,有触摸时高亮
UIButtonTypeDetailDisclosure, 前面带“!”图标按钮 ,默认文字颜色为蓝色,有触摸时高亮
UIButtonTypeInfoLight, 为感叹号“!”圆形按钮
UIButtonTypeInfoDark, 为感叹号“!”圆形按钮
UIButtonTypeContactAdd, 前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时高
UIButtonTypePlain
UIButtonTypeRoundedRect
*/
SDWebImage
#import <SDWebImage/UIButton+WebCache.h>
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""]];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] options:SDWebImageLowPriority completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
[button sd_setBackgroundImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
}];
- 避免短时间多次点击按钮
》》》 》》》 1. 创建分类文件
UIButton+FQTimeButton.h
#import <UIKit/UIKit.h>
#define defaultInterval 2.0// 默认间隔时间
@interface UIButton (FQTimeButton)
// 设置点击时间间隔
@property (nonatomic, assign) NSTimeInterval timeInterVal;
@end
UIButton+FQTimeButton.m
#import "UIButton+FQTimeButton.h"
@interface UIButton()
// 用来判断 是否去执行方法
@property (nonatomic, assign) BOOL isExcuteEvent;
@end
@implementation UIButton (FQTimeButton)
// main方法之前会调用d所有类的load方法
+ (void)load{
// 仅执行一次,避免多次替换出错
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 交换自定义方法和系统方法
SEL oldSel = @selector(sendAction:to:forEvent:);
SEL newSel = @selector(newSendAction:to:forEvent:);
Method oldMethod = class_getInstanceMethod(self, oldSel);
Method newMethod = class_getInstanceMethod(self, newSel);
BOOL isAdd = class_addMethod(self, oldSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
if (isAdd) {
class_replaceMethod(self, newSel, method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
}else{
method_exchangeImplementations(oldMethod, newMethod);
}
});
}
// 触发按钮事件时会调用
- (void)newSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
self.timeInterVal = self.timeInterVal == 0? defaultInterval:self.timeInterVal;
if (self.isExcuteEvent) return;
if (self.timeInterVal > 0) {
self.isExcuteEvent = true;
[self performSelector:@selector(setIsExcuteEvent:) withObject:nil afterDelay:self.timeInterVal];
}
}
[self newSendAction:action to:target forEvent:event];
}
// 动态关联属性
- (NSTimeInterval)timeInterVal{
return [objc_getAssociatedObject(self, _cmd) doubleValue];
}
- (void)setTimeInterVal:(NSTimeInterval)timeInterVal{
objc_setAssociatedObject(self, @selector(timeInterVal), @(timeInterVal), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)setIsExcuteEvent:(BOOL)isExcuteEvent{
objc_setAssociatedObject(self, @selector(isExcuteEvent), @(isExcuteEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isExcuteEvent{
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
@end
》》》 》》》 2. 使用
#import "UIButton+FQTimeButton.h"
-(UIButton *)saveButton{
if(!_saveButton){
_saveButton=[UIButton new];
[_saveButton setTimeInterVal:3.5]; // 设置3.5秒内不可重复点击
[_saveButton setTitle:@"保存" forState:UIControlStateNormal];
[_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_saveButton.titleLabel setFont:kFontBold(14)];
[_saveButton setBackgroundColor:[UIColor colorWithHex:0xFB5752]];
[_saveButton.layer setMasksToBounds:true];
[_saveButton.layer setCornerRadius:kEstimateWidth(50)/2.0];
[_saveButton addTarget:self action:@selector(fq_handleSave:) forControlEvents:UIControlEventTouchUpInside];
}
return _saveButton;
}
- 扩大按钮的点击区域
创建一个继承自UIButton的子类,需要扩大点击区域时,继承该类
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
CGRect rect=self.bounds;
CGFloat width=rect.size.width;
CGFloat height=rect.size.height;
rect=CGRectInset(rect, -width*0.5, -height*0.5); // 宽高各扩大了一半
return CGRectContainsPoint(rect, point);
}
2. 手势
- 系统自带手势
- 点击手势 UITapGestureRecognizer
UITapGestureRecognizer *tapG=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
tapG.numberOfTapsRequired=1; // 点击次数(默认:1)
tapG.numberOfTouchesRequired=1; // 点击手指数(默认:1)
[imgV1 addGestureRecognizer:tapG]; // imgV 必须设置为可交互
-(void)handleTap:(UITapGestureRecognizer *)tapG{
//
UIImageView *imgV=(UIImageView *)tapG.view;
}
- 长按手势 UILongPressGestureRecognizer
// 长按手势
UILongPressGestureRecognizer *longPressG=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
longPressG.numberOfTouchesRequired=1; // 手指数(默认:1)
// longPressG.numberOfTapsRequired=1; // 点击数(默认:0)
longPressG.minimumPressDuration=1; // 最小的长按时间(默认:0.5)
longPressG.allowableMovement=10; // 最大的移动距离(默认:10ps)
[[UIImageView new]addGestureRecognizer:longPressG];
-(void)handleLongPress:(UILongPressGestureRecognizer *)longPressG{
//
UIImageView *imgV=(UIImageView *)longPressG.view;
if(tapG.state==UIGestureRecognizerStateEnded){
}else if(tapG.state==UIGestureRecognizerStateBegan){
}
}
- 滑动手势 UIPanGestureRecognizer
// 拖动手势
UIPanGestureRecognizer *panG=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[panG setMaximumNumberOfTouches:2]; // 最小手指数
[panG setMinimumNumberOfTouches:1]; // 最大手指数
[[UIImageView new]addGestureRecognizer:panG];
-(void)handlePan:(UIPanGestureRecognizer *)panG{
//
UIButton *itemButton=(UIButton *)panG.view;
CGPoint transP=[panG translationInView:self];
NSLog(@"%f %f",transP.x,transP.y);
//
if(transP.x>0&&transP.x<294-95){
[UIView animateWithDuration:0.1 animations:^{
[itemButton setTransform:CGAffineTransformMakeTranslation(transP.x, 0)];
}];
}else if(transP.x>=294){
[self.subject sendNext:@(100)];
}
//
if(panG.state==UIGestureRecognizerStateEnded){
[UIView animateWithDuration:0.1 animations:^{
[itemButton setTransform:CGAffineTransformIdentity];
}];
}
// [panG setTranslation:CGPointZero inView:self.view]; // reset
}
- 扫动手势 UISwipeGestureRecognizer
// 扫动手势
UISwipeGestureRecognizer *swipeG=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
swipeG.numberOfTouchesRequired=1; // 手指数(默认:1)
swipeG.direction=UISwipeGestureRecognizerDirectionLeft; // 扫动方向(如果多个方向,则添加多个手势)
[[UIImageView new]addGestureRecognizer:swipeG];
-(void)handleSwipe:(UISwipeGestureRecognizer *)swipeG{
//
UIImageView *imgV=(UIImageView *)swipeG.view;
}
- 缩放手势 UIPinchGestureRecognizer
// 缩放手势
UIPinchGestureRecognizer *pinchG=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[pinchG setScale:0.5]; // 缩放比例
// pinchG.velocity(readOnly)
[[UIImageView new]addGestureRecognizer:pinchG];
-(void)handlePinch:(UIPinchGestureRecognizer *)pinchG{
//
UIImageView *imgV=(UIImageView *)pinchG.view;
// pinchG.scale
}
- 旋转手势 UIRotationGestureRecognizer
// 旋转手势
UIRotationGestureRecognizer *roteG=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRote:)];
[roteG setRotation:M_PI_2]; // 旋转角度
// roteG.velocity
[[UIImageView new]addGestureRecognizer:roteG];
-(void)handleRote:(UIRotationGestureRecognizer *)roteG{
//
UIImageView *imgV=(UIImageView *)roteG.view;
// roteG.rotation
}
- 自定义手势
: UIGestureRecognizer
#import <UIKit/UIKit.h>
@interface CustomGestureRecognizer : UIGestureRecognizer
@end
#import "CustomGestureRecognizer.h"
#import <UIkit/UIGestureRecognizerSubclass.h>
@implementation CustomGestureRecognizer
-(instancetype)initWithTarget:(id)target action:(SEL)action{
return [super initWithTarget:target action:action];
}
// 开始触摸时调用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 触摸移动时调用
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 触摸结束时调用
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 触摸取消时调用
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
@end
基类 UIGestureRecognizer
UIGestureRecognizer : NSObject
所有手势的基类,不直接使用
// 创建手势 (添加self为target)
UIGestureRecognizer *g=[[UIGestureRecognizer alloc]initWithTarget:self action:@selector(handleG)];
// 添加 其他VC的targetAction
[g addTarget:OtherVC action:@selector(handleG)];
// 移除 其他VC的targetAction
[g removeTarget:OtherVC action:@selector(handleG)];
// 获取 手势操作的View(readonly)
UIView *view=g.view;
// 获取 手势的当前状态(readOnly)
UIGestureRecognizerState state=g.state;
// 获取 手指数(readonly)
NSUInteger numberOfTouches=g.numberOfTouches;
// 获取 触摸点的坐标
CGPoint touchPoint2=[g locationInView:g.view];
// 获取 指定触摸点的坐标
CGPoint touchPoint=[g locationOfTouch:0 inView:g.view];
// 当两个手势有【冲突】时,手势1只会在手势2失败时才会有效
[g1 requireGestureRecognizerToFail:g2];
// 设置 手势名
[g setName:@""];
// 获取 手势名
NSString *name=g.name;
// 设置 是否可用(默认:true)
[g setEnabled:true];
// 获取 是否可用
BOOL isEnabled=g.isEnabled;
// 设置 是否同时考虑不同手势(true会忽略不同手势)
[g setRequiresExclusiveTouchType:true];
// 设置 允许触摸的类型
[g setAllowedTouchTypes:@[[NSNumber numberWithInt:0]]];
// 设置 允许按压的类型
[g setAllowedPressTypes:@[[NSNumber numberWithInt:0]]];
手势识别和触摸事件相互独立,但可通过以下3个属性互相影响:
在3个属性都处于默认值的情况下,如果触摸window,首先由window上最先符合条件的控件(该控件记为hit-test view)接收到该touch并触发触摸事件touchesBegan。同时如果某个控件的手势识别器接收到了该touch,就会进行识别。手势识别成功之后发送触摸事件touchesCancelled给hit-testview,hit-test view不再响应touch。
/*
默认为YES,这种情况下当手势识别器识别到touch之后,会发送touchesCancelled给hit-testview以取消hit-test view对touch的响应,这个时候只有手势识别器响应touch。
若设置成NO时,手势识别器识别到touch之后不会发送touchesCancelled给hit-test,这个时候手势识别器和hit-test view均响应touch。
*/
[g setCancelsTouchesInView:true];
/*
默认是NO,这种情况下当发生一个touch时,手势识别器先捕捉到到touch,然后发给hit-testview,两者各自做出响应。
若设置为YES,手势识别器在识别的过程中(注意是识别过程),不会将touch发给hit-test view,即hit-testview不会有任何触摸事件。只有在识别失败之后才会将touch发给hit-testview,这种情况下hit-test view的响应会延迟约0.15ms。
*/
[g setDelaysTouchesBegan:true];
/*
默认为YES。这种情况下发生一个touch时,在手势识别成功后,发送给touchesCancelled消息给hit-testview,手势识别失败时,会延迟大概0.15ms,期间没有接收到别的touch才会发送touchesEnded。
若设置为NO,则不会延迟,即会立即发送touchesEnded以结束当前触摸。
*/
[g setDelaysTouchesEnded:true];
dele
// dele. <UIGestureRecognizerDelegate>
[g setDelegate:self];
#pragma mark dele -UIGestureRecognizerDelegate
/*
是否允许手势
*/
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return true;
}
/*
是否支持多手势(解决有水平方向滚动的ScrollView时边缘返回手势失效的问题)
false默认,识别到第一个手势则停止识别第二个手势
true,则识别到第一个手势,还会去识别第二个手势
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
/*
当第一个手势gestureRecognizer和第二个手势otherGestureRecognizer发生冲突时,第二个失效
*/
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
/*
当第一个手势和第二个手势发生冲突时,第一个失效
*/
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return true;
}
/*
是否允许 手势识别(默认:true)
*/
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return false; // 禁止识别
}
/*
开始识别手势时调用(可用于:控件某范围有效)
*/
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return true;
}
- 常用
- UIViewController 或 UIView 中可覆写
// UIViewController:UIResponder
// 开始触摸后调用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 触摸并移动后调用
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 触摸结束后调用
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
// 触摸取消后调用
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
}
- 获取到侧滑手势
// 获取侧滑手势,侧滑手势继承于:UIPanGestureRecognizer
NSArray *gestureArray = self.navigationController.view.gestureRecognizers;
for (UIGestureRecognizer *gesture in gestureArray) {
if ([gesture isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
}
}
- 在VC禁止侧滑返回上一页
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}