iOS事件(二)手势
2019-05-09 本文已影响13人
追逐_chase
iOS手势
轻拍手势UITapGestureRecognizer
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hangdletap:)];
[self.imageView addGestureRecognizer:tap];
}
- (void)hangdletap:(UITapGestureRecognizer *)tap {
}
长按手势UILongPressGestureRecognizer
- 开发中使用 聊天 发送语音 使用
- (void)viewDidLoad {
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(hangdleLongPress:)];
[self.imageView addGestureRecognizer:longPress];
}
- (void)hangdleLongPress:(UILongPressGestureRecognizer *)log {
//状态变化
if ( log.state == UIGestureRecognizerStateBegan) {
} else if (log.state == UIGestureRecognizerStateCancelled){
}else if (log.state == UIGestureRecognizerStateEnded){
}
}
05_long.gif
轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeAction)];
[self.view addGestureRecognizer:swipe];
- (void)handleSwipeAction{
NSLog(@"handleSwipeAction----");
}
捏合手势UIPinchGestureRecognizer
- (void)viewDidLoad {
[super viewDidLoad];
UIPinchGestureRecognizer *pich = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(hangdlepich:)];
[self.imageView addGestureRecognizer:pich];
}
- (void)hangdlepich:(UIPinchGestureRecognizer *)pich{
NSLog(@"----------");
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pich.scale, pich.scale);
[pich setScale:1];
}
04_pich.gif
拖拽手势UIPanGestureRecognizer
- (void)viewDidLoad {
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(hangdlePan:)];
[self.imageView addGestureRecognizer:pan];
}
- (void)hangdlePan:(UIPanGestureRecognizer *)pan {
//获取到的点是相对于q起始位置
CGPoint point = [pan translationInView:self.imageView];
//z形变
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
//把原来的职位0
[pan setTranslation:CGPointZero inView:self.imageView];
}
03_pan.gif
旋转手势
- (void)viewDidLoad {
[super viewDidLoad];
UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(hangdlerota:)];
[self.imageView addGestureRecognizer:rota];
[self longPress];
}
- (void)hangdlerota:(UIRotationGestureRecognizer *)rota{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rota.rotation);
[rota setRotation:0];
}
06_rota.gif
上述这些手势继承与
UIGestureRecognizer
都有这个父类的代理方法
//同时接受多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
手势 demo
07_demo.gif核心代码
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CCDragVC : UIViewController
/*
readonly 外界不能更改这些属性的值,但是内部可以访问 _mainView 进行操作
*/
///正常显示的View
@property (nonatomic, weak, readonly) UIView *mainView;
///左边的View
@property (nonatomic, weak, readonly) UIView *leftView;
///右边的view
@property (nonatomic, weak, readonly) UIView *rightView;
///是否根据导航内容布局
@property(nonatomic,assign) UIRectEdge edgesForExtendedLayout;
@end
NS_ASSUME_NONNULL_END
#import "CCDragVC.h"
#define screenW [UIScreen mainScreen].bounds.size.width
#define maxY 100
#define targetR 270
#define targetL -270
@interface CCDragVC ()
@end
@implementation CCDragVC
- (void)viewDidLoad {
[super viewDidLoad];
//布局子控件
[self layoutChildView];
//添加手势
[self setupGesture];
}
//MARK:--布局子控件
- (void)layoutChildView{
CGRect viewFrame = self.view.bounds;
//左边
UIView *leftView = [[UIView alloc] init];
leftView.frame = viewFrame;
[self.view addSubview:leftView];
_leftView = leftView;
//右边
UIView *rightView = [[UIView alloc] initWithFrame:viewFrame];
[self.view addSubview:rightView];
_rightView = rightView;
//主View
UIView *mainView = [[UIView alloc] initWithFrame:viewFrame];
[self.view addSubview:mainView];
_mainView = mainView;
//设置颜色
_leftView.backgroundColor = [UIColor purpleColor];
_rightView.backgroundColor = [UIColor orangeColor];
_mainView.backgroundColor = [UIColor lightGrayColor];
}
//MARK:--添加手势
- (void)setupGesture {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanAction:)];
[self.mainView addGestureRecognizer:pan];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)];
[self.view addGestureRecognizer:tap];
}
//MARK:--手势方法处理
- (void)handlePanAction:(UIPanGestureRecognizer *)pan {
//获取到偏移的点
CGPoint point = [pan translationInView:self.mainView];
//计算
self.mainView.frame = [self getFrameWithOffset:point.x];
//判断是左滑还是油花
if (self.mainView.frame.origin.x > 0) {
self.rightView.hidden = YES;
}else{
self.rightView.hidden = NO;
}
//给mainView一个定位
if (pan.state == UIGestureRecognizerStateEnded) {
CGFloat target = 0;
if (self.mainView.frame.origin.x > screenW *0.5) {
//判断右侧
target = targetR;
}else if (CGRectGetMaxX(self.mainView.frame) < screenW *0.5){
//判断左侧
target = targetL;
}
CGFloat offset = target - self.mainView.frame.origin.x;
[UIView animateWithDuration:0.5 animations:^{
self.mainView.frame = [self getFrameWithOffset:offset];
}];
}
//复位
[pan setTranslation:CGPointZero inView:self.mainView];
}
- (void)handleTapAction:(UITapGestureRecognizer *)tap {
[UIView animateWithDuration:0.25 animations:^{
self.mainView.frame = self.view.bounds;
}];
}
//MARK:---计算frame
- (CGRect)getFrameWithOffset:(CGFloat)offsetX{
CGRect frame = _mainView.frame;
//计算偏移量
frame.origin.x += offsetX;
//计算y的偏移量
//设置一个最大的Y值,当达到这个值时,mianView被拖拽到一个屏幕的宽度
CGFloat offsetY = (frame.origin.x * maxY)/screenW;
frame.origin.y = fabs(offsetY);
CGFloat screenHeight = 0;
if (self.edgesForExtendedLayout == UIRectEdgeNone) {
screenHeight = [UIScreen mainScreen].bounds.size.height - 64;
}else{
screenHeight = [UIScreen mainScreen].bounds.size.height;
}
//计算主View缩小的g变化的高度
frame.size.height = (screenHeight - fabs(offsetY) * 2);
return frame;
}
@end
只要控制器继承这个CCDragVC,然后分别在器属性上添加对应的View就可以使用,
注意
:在添加View的时候注意,添加子控制器
XXMainVC
继承CCDragVC
, 下面的代码
@implementation XXMainVC
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航的内容区域
self.edgesForExtendedLayout = UIRectEdgeNone;
//这是mianView
CCTabVC *mainVC = [[CCTabVC alloc] init];
[self addChildViewController:mainVC];
[self.mainView addSubview:mainVC.view];
}