UITapGestureRecognizer、 UILongPressGestureRecongnizer是最基础的两个手势。
https://my.mindnode.com/zV4BtjhSHzZYzF4cGc2RNdoe2CyJqbi9ZN5RpEpg
给一个视图添加Tap手势:
//手势的定义
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:selfaction:@selector(tapGestureAction:)];
[self.rootView.imageView addGestureRecognizer:tapGesture];
//双击(多次点击,看个人设置)
tapGesture.numberOfTapsRequired = 2;
添加LongPress手势:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureAction:)];
//默认时间是0.5
[self.rootView.imageView addGestureRecognizer:longPressGesture];
添加Pan手势:
//平移
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
[self.rootView.imageView addGestureRecognizer:panGesture];
响应方法:
//平移
-(void)panGestureAction:(UIPanGestureRecognizer *)sender
{
//translation in the coordinate system of the specified view.
//point就是你拖动过程中的距离差。
CGPoint point = [sender translationInView:self.view];
//移动center值。
self.rootView.imageView.center = CGPointMake(self.rootView.imageView.center.x + point.x, self.rootView.imageView.center.y + point.y);
//把translation设置为0
[sender setTranslation:CGPointZeroinView:self.view];
}
添加Rotation手势:
//旋转
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureAction:)];
[self.rootView.imageView addGestureRecognizer:rotationGesture];
响应事件:
//旋转
-(void)rotationGestureAction:(UIRotationGestureRecognizer *)sender
{
//参考点:原始位置
// self.rootView.imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
//参考点:上一次旋转到的位置和加速度
self.rootView.imageView.transform = CGAffineTransformRotate(self.rootView.imageView.transform, sender.rotation);
sender.rotation = 0;
NSLog(@"%f",sender.rotation);
}
添加Pinch事件:
//缩放
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureAction:)];
[self.rootView.imageView addGestureRecognizer:pinchGesture];
响应事件:
//缩放
-(void)pinchGestureAction:(UIPinchGestureRecognizer *)sender
{
//
// self.rootView.imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
self.rootView.imageView.transform = CGAffineTransformScale(self.rootView.imageView.transform, sender.scale, 1);
sender.scale = 1;
}
添加一个开关事件:
添加视图:
UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectMake(10, 280, 50, 30)];
self.switchView = switchView;
[self addSubview:switchView];
控制器中设置:
//开关
[self.rootView.switchView addTarget:self action:@selector(switchViewAction:) forControlEvents:UIControlEventValueChanged];
控制器中响应事件:
//开关
-(void)switchViewAction:(UISwitch *)sender
{
if (sender.on == 0 ) {
self.rootView.imageView.userInteractionEnabled = NO;
}else
{
self.rootView.imageView.userInteractionEnabled = YES;
}
NSLog(@"%@",sender);
}
添加事件:
添加视图:
UIStepper *stepperView = [[UIStepper alloc]initWithFrame:CGRectMake(10, 330, 50, 30)];
//stepper最小值
stepperView.minimumValue = 10;
//stepper最大值
stepperView.maximumValue = 200;
//当前值
stepperView.value = 100;
//每次递增或递减的值
stepperView.stepValue = 10;
self.stepperView = stepperView;
[self addSubview:stepperView];
控制器中设置:
//缩放
[self.rootView.stepperView addTarget:self action:@selector(stepperViewAction:) forControlEvents:UIControlEventValueChanged];
响应事件:
//缩放
-(void)stepperViewAction:(UIStepper *)sender
{
//长宽 同等增加或减少,位置以上一次为参考。
self.rootView.imageView.frame = CGRectMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y, sender.value, sender.value);
NSLog(@"%d",(int)sender.value);
}
源代码:
#import "AppDelegate.h"
//引入头文件
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//复习UI 前六天 的知识点
#pragma mark ------------- 创建 主窗口 ---------------------------
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
#pragma mark ---------- 创建 根视图控制器 -----------------------------
self.window.rootViewController = [[RootViewController alloc]init];
[self.window addSubview:self.window.rootViewController.view];
#pragma mark -------- UIView ----------------------------
// properties
// 1.backgroundColor
// 2.frame
// 3.bounds
// 4.center
// 5.subViews
// 6.superView
// 7.hidden
// 8.alpha
// 9.text
// 10.tag
// functions
// 1.[fatherView addSubViews: sonView]
// 2.[subView removefromSuperView]
// 3.[superView insertSubView: subView]
// 4.[superView bringSubViewToFront: subView]
// 5.[superView sendSubViewToBack: subView]
// 6.exchange TwoSubView with index.
#pragma mark UILabel UITextField UIButton Delegate 程序状态
//NSObject:子类-》UIImage、UIColor、UIFont、UIScreen
//NSObject子类-》UIResponder-》UIView:UIWindow、UILabel、UIControl
//UIControl子类-》UITextField、UIButton
//UILabel
//UITextField
//UIButton
//Delegate:1.设置代理 2.遵循协议(1..h 2..m extension) 3.实现协议方法
//FirstResponder :1. resignFirstResponder 2.becomeFirstResponder
//程序状态:启动完成-》将要进入活跃区-》进入前台-》将要进入非活跃区-》进入后台-》程序终止。
//AppDelegate 必须遵循UIApplicationDelegate
#pragma mark 自定义视图 控制器 视图控制器
//自定义视图:1.继承自UIView 创建 LTView
// 2.重写初始化方法,在方法内部添加视图
// 3.还可以添加自定义初始化方法,把重要的值 作为参数
// 4.某些情况需要使用的值,写成属性(重写setter和getter方法)
// 5.delegate的使用
//控制器 :1.创建文件-》LoginViewController 和 LoginView
// 2.使用自定义视图 替换 控制器自带的view
// 2.1.1在LoginViewController.m引入LoginViewController.h
// 2.1.2在延展声明LoginViewController对象属性
// 2.1.3重写loadView方法
// 3.在控制器中处理事件
//视图控制器:MVC模式
// 作用:处理事件
// 设置代理
// 检测屏幕旋转-》总开关、内部设置supportedInterfaceOrientation、执行方法-》viewWillTransition...
// 获取内存警告:if([self isLoad] && (self.view.window == nil)){
// self.view = nil;//释放,在setter内部释放。
// }
#pragma mark 事件、响应者链
//事件:1. 摇晃-》motionBegan:withEvent:
// -》motionEnded:withEvent:
// -》motionCancelled:withEvent:
// 2.远程控制
// 3.触摸:-》touchesBegan:withEvcent:
// -》touchesMoved:withEvent:
// -》touchesEnded:withEvent:
// -》touchesCancelled:withEvent:
// UItouch -》locationInView->self 或者 ->self.window
//响应者链 :屏幕-》UIApplication-》UIWindow-》UIViewController-》UIView-》subView
// :检测响应者
// :确定响应者
// :阻断响应者链:userInteractionEnable 默认关闭的:UILabel\UIImageView 其他开。
#pragma mark 高内聚、低耦合、Target-action 、delegate 、UIImageView 、手势、UIView
//Target-Action模式:仿造UIButton 的功能,继承自UIView,重写初始化方法,以及声明一个addTarget:(id)target action:(SEL)action controlEvents:(UIControlEvents)controlEvent;方法,在.m中重写touches触发事件的方法。
//delegate模式:仿造UITextField 的功能,继承自UITextField,重写初始化方法,以及在.h中声明协议以及协议里的内容,声明一个delegate属性,在.m文件中,重写触摸事件,在事件中,判断该代理是否为空以及该代理能否响应协议方法,能得话,就让代理执行协议方法。在控制器中:1.设置代理 2.遵循协议 3.实现协议里的方法。
//UIImageView userInteractionEnable = NO;
//手势:tap\longPress\swipe\rotation\pan\screenEdgePan\pinch
//UIView transform ->CGAffineTransformMakeRotation
// ->CGAffineTransformRotation
// scale
// pan -> translationInView
#pragma mark 到目前为止 所学类的归类
return YES;
}
==============================================================
#import
@class MyTextField;
//声明协议
@protocol MyTextFieldDelegate
//声明协议内容
-(void)textFieldBegin:(MyTextField *)textField;
-(void)textFieldIng:(MyTextField *)textField;
-(void)textFieldEnd:(MyTextField *)textField;
@end
@interface MyTextField : UIView
//声明delegate属性
@property(nonatomic,strong) id
@end
==================================================================
#import "MyTextField.h"
@implementation MyTextField
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
//重写触摸事件
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//代理不为空 并且 代理能够响应协议里的方法
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(textFieldEnd:)]) {
[_delegate textFieldEnd:self];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(textFieldBegin:)]) {
[_delegate textFieldBegin:self];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(textFieldIng:)]) {
[_delegate textFieldIng:self];
}
}
@end
===================================================================
#import
@interface MyButton : UIView
-(void)addTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents;
@end
=================================================================
#import "MyButton.h"
@interface MyButton ()
{
id _target;
SEL _action;
UIControlEvents _controlEvents;
}
@end
@implementation MyButton
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
-(void)addTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents{
_target = target;
_action = action;
_controlEvents = controlEvents;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_controlEvents == UIControlEventTouchUpInside) {
[_target performSelector:_action withObject:self];
}
}
@end
================================================================
#import "RootViewController.h"
//引入头文件
#import "RootView.h"
#import "MyButton.h"
#import "MyTextField.h"
//遵循协议
@interface RootViewController ()
//声明私有属性
@property(nonatomic,strong) RootView *rootView;
@end
@implementation RootViewController
#pragma mark ---------- 自定义视图 替换 控制器自带的view ----------------
-(void)loadView
{
self.rootView = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
}
//控制器 事件处理 方法
- (void)viewDidLoad {
[super viewDidLoad];
[self.rootView.myButton addTarget:self action:@selector(myButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//设置代理
self.rootView.myTextField.delegate = self;
//手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)];
[self.rootView.imageView addGestureRecognizer:tapGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureAction:)];
//默认是0.5
[self.rootView.imageView addGestureRecognizer:longPressGesture];
//平移
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
[self.rootView.imageView addGestureRecognizer:panGesture];
//旋转
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureAction:)];
[self.rootView.imageView addGestureRecognizer:rotationGesture];
//缩放
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureAction:)];
[self.rootView.imageView addGestureRecognizer:pinchGesture];
//开关
[self.rootView.switchView addTarget:self action:@selector(switchViewAction:) forControlEvents:UIControlEventValueChanged];
//缩放
[self.rootView.stepperView addTarget:self action:@selector(stepperViewAction:) forControlEvents:UIControlEventValueChanged];
}
//缩放
-(void)stepperViewAction:(UIStepper *)sender
{
//长宽 同等增加或减少,位置以上一次为参考。
self.rootView.imageView.frame = CGRectMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y, sender.value, sender.value);
NSLog(@"%d",(int)sender.value);
}
//开关
-(void)switchViewAction:(UISwitch *)sender
{
if (sender.on == 0 ) {
self.rootView.imageView.userInteractionEnabled = NO;
}else
{
self.rootView.imageView.userInteractionEnabled = YES;
}
NSLog(@"%@",sender);
}
//缩放
-(void)pinchGestureAction:(UIPinchGestureRecognizer *)sender
{
//
// self.rootView.imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
self.rootView.imageView.transform = CGAffineTransformScale(self.rootView.imageView.transform, sender.scale, 1);
sender.scale = 1;
}
//旋转
-(void)rotationGestureAction:(UIRotationGestureRecognizer *)sender
{
//参考点:原始位置
// self.rootView.imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
//参考点:上一次旋转到的位置和加速度
self.rootView.imageView.transform = CGAffineTransformRotate(self.rootView.imageView.transform, sender.rotation);
sender.rotation = 0;
NSLog(@"%f",sender.rotation);
}
//平移
-(void)panGestureAction:(UIPanGestureRecognizer *)sender
{
//translation in the coordinate system of the specified view.
//point就是你拖动过程中的距离差。
CGPoint point = [sender translationInView:self.view];
//移动center值。
self.rootView.imageView.center = CGPointMake(self.rootView.imageView.center.x + point.x, self.rootView.imageView.center.y + point.y);
//把translation设置为0
[sender setTranslation:CGPointZero inView:self.view];
NSLog(@"%@",NSStringFromCGPoint(point));
NSLog(@"%@",NSStringFromCGPoint(self.rootView.imageView.center));
NSLog(@"pan");
}
//长按
-(void)longPressGestureAction:(UILongPressGestureRecognizer *)sender
{
NSLog(@"longPress");
}
//单击
-(void)tapGestureAction:(UITapGestureRecognizer *)sender
{
//双击
sender.numberOfTapsRequired = 2;
NSLog(@"tap");
}
//实现协议方法
-(void)textFieldIng:(MyTextField *)textField
{
NSLog(@"ing");
}
-(void)textFieldBegin:(MyTextField *)textField
{
NSLog(@"begin");
}
-(void)textFieldEnd:(MyTextField *)textField
{
NSLog(@"end");
}
//Button点击事件
-(void)myButtonAction:(MyButton *)sender
{
NSLog(@"%@",sender);
}
==============================================================
#import
@class MyButton;
@class MyTextField;
@interface RootView : UIView
@property(nonatomic,strong) MyButton *myButton;
@property(nonatomic,strong) MyTextField *myTextField;
@property(nonatomic,strong) UIImageView *imageView;
@property(nonatomic,strong) UISwitch *switchView;
@property(nonatomic,strong) UIStepper *stepperView;
@end
===========================================================
#import "RootView.h"
#import "MyButton.h"
#import "MyTextField.h"
@implementation RootView
//重写初始化方法
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self addAllViews];
}
return self;
}
//自定义添加视图方法
-(void)addAllViews
{
MyButton *myButton = [[MyButton alloc]initWithFrame:CGRectMake(10, 20, 100, 30)];
myButton.backgroundColor = [UIColor cyanColor];
self.myButton = myButton;
[self addSubview:myButton];
MyTextField *myTextField = [[MyTextField alloc]initWithFrame:CGRectMake(10, 100, 100, 30)];
myTextField.backgroundColor = [UIColor blackColor];
self.myTextField = myTextField;
[self addSubview:myTextField];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ff.png"]];
imageView.frame = CGRectMake(10, 150, 100, 100);
imageView.userInteractionEnabled = YES;
self.imageView = imageView;
[self addSubview:imageView];
#pragma mark 开关
UISwitch *switchView = [[UISwitch alloc]initWithFrame:CGRectMake(10, 280, 50, 30)];
self.switchView = switchView;
[self addSubview:switchView];
#pragma mark
UIStepper *stepperView = [[UIStepper alloc]initWithFrame:CGRectMake(10, 330, 50, 30)];
//stepper最小值
stepperView.minimumValue = 10;
//stepper最大值
stepperView.maximumValue = 200;
//当前值
stepperView.value = 100;
//每次递增或递减的值
stepperView.stepValue = 10;
self.stepperView = stepperView;
[self addSubview:stepperView];
}
@end
=====================================================
需要自己拷贝一个图片进文件夹中。