0829-0613 手势识别&Layer
0829-0613 手势识别&Layer
手势识别后一般采用核心动画来操作 不用基本代码来实心
// NSValue *v = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -200, 0)];
// [self.iconView.layer setValue:v forKeyPath:@"transform"];
[self.iconView.layer setValue:@(-100) forKeyPath:@"transform.translation.x"];
[pan setTranslation:CGPointZero inView:pan.view];
layer 其他属性
layer.contens 辅助视图 保存的是UI元素 转成CG元素 再转成id
self.customView.layer.contents = (id)[UIImage imageNamed:@"me"].CGImage;
self.iconView.layer.borderColor = [UIColor purpleColor].CGColor;
self.iconView.layer.cornerRadius = 10;
// 设置超出主图层的部分剪切掉
// self.customView.clipsToBounds = YES;
----------------
清扫和长按
//
// NJViewController.m
// 01-长按+轻扫
//
// Created by apple on 14-6-13.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "NJViewController.h"
@interface NJViewController ()
@property (weak, nonatomic) IBOutlet UIView *customView;
@end
@implementation NJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 向上
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] init];
// 设置轻扫的方向
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.customView addGestureRecognizer:swipe];
[swipe addTarget:self action:@selector(swipeView)];
// 向下
UISwipeGestureRecognizer *swipe2 = [[UISwipeGestureRecognizer alloc] init];
// 设置轻扫的方向
swipe2.direction = UISwipeGestureRecognizerDirectionDown;
[self.customView addGestureRecognizer:swipe2];
[swipe2 addTarget:self action:@selector(swipeView2)];
// 左边
UISwipeGestureRecognizer *swipe3 = [[UISwipeGestureRecognizer alloc] init];
// 设置轻扫的方向
swipe3.direction = UISwipeGestureRecognizerDirectionLeft;
[self.customView addGestureRecognizer:swipe3];
[swipe3 addTarget:self action:@selector(swipeView3)];
// 右边
UISwipeGestureRecognizer *swipe4 = [[UISwipeGestureRecognizer alloc] init];
// 设置轻扫的方向
swipe4.direction = UISwipeGestureRecognizerDirectionRight;
[self.customView addGestureRecognizer:swipe4];
[swipe4 addTarget:self action:@selector(swipeView4)];
}
- (void)swipeView4
{
NSLog(@"轻扫事件右");
}
- (void)swipeView3
{
NSLog(@"轻扫事件左");
}
- (void)swipeView2
{
NSLog(@"轻扫事件下");
}
- (void)swipeView
{
NSLog(@"轻扫事件上");
}
- (void)test
{
// 长按事件
// 1.创建手势识别器
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
// 1.1设置长按手势识别器的属性
// longPress.minimumPressDuration = 5;
// 手指按下后事件响应之前允许手指移动的偏移位
longPress.allowableMovement = 50;
// 2.添加手势识别器到View
[self.customView addGestureRecognizer:longPress];
// 3.监听手势识别器
[longPress addTarget:self action:@selector(longPressView)];
}
-(void)longPressView
{
NSLog(@"长按事件");
}
@end
----------------------------
旋转+缩放
// // NJViewController.m #import "NJViewController.h" @interface NJViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; [self pichTest]; [self rotationTest]; } // 该方法返回的BOOL值决定了view是否能够同时响应多个手势 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { NSLog(@"%@ - %@", gestureRecognizer.class, otherGestureRecognizer.class); return YES; } - (void)pichTest { // 捏合手势 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] init]; pinch.delegate = self; [self.iconView addGestureRecognizer:pinch]; [pinch addTarget:self action:@selector(pinchView:)]; } - (void)pinchView:(UIPinchGestureRecognizer *)pinch { // NSLog(@"捏合事件 %.1f", pinch.scale); // self.iconView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale); // 1.0 * 0.9 self.iconView.transform = CGAffineTransformScale(self.iconView.transform, pinch.scale, pinch.scale); pinch.scale = 1.0; } - (void)rotationTest { // 旋转 UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] init]; gesture.delegate = self; [self.iconView addGestureRecognizer:gesture]; [gesture addTarget:self action:@selector(rotationView:)]; } - (void)rotationView:(UIRotationGestureRecognizer *)gesture { // NSLog(@"旋转事件 %.1f", gesture.rotation); // 每次从最初的位置开始 // self.iconView.transform = CGAffineTransformMakeRotation(gesture.rotation); // 在传入的transform基础上递增一个弧度 self.iconView.transform = CGAffineTransformRotate(self.iconView.transform, gesture.rotation); // 将旋转的弧度清零(注意不是将图片旋转的弧度清零, 而是将当前手指旋转的弧度清零) gesture.rotation = 0;// 如果理解不了 , 记住就OK } @end
--------------------
拖拽
// // NJViewController.m #import "NJViewController.h" @interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init]; [self.customView addGestureRecognizer:pan]; [pan addTarget:self action:@selector(panView:)]; } - (void)panView:(UIPanGestureRecognizer *)pan { // 返回的值是以手指按下的点为原点 // 1 2 3 4 5 CGPoint point = [pan translationInView:pan.view]; NSLog(@"拖拽事件 %@", NSStringFromCGPoint(point)); CGPoint temp = self.customView.center; temp.x += point.x; temp.y += point.y; self.customView.center = temp; // 理解不了就记住就OK [pan setTranslation:CGPointZero inView:pan.view]; } @end
-------------
CALayer基本使用
// // NJViewController.m // 04-CALayer基本使用 #import "NJViewController.h" @interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @property (weak, nonatomic) IBOutlet UIImageView *iconView; @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* // self.iconView.transform = CGAffineTransformMakeTranslation(0, -100); // self.iconView.layer.transform = CATransform3DMakeTranslation(0, -100, 0); // NSValue *v = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, -200, 0)]; // [self.iconView.layer setValue:v forKeyPath:@"transform"]; [self.iconView.layer setValue:@(-100) forKeyPath:@"transform.translation.x"]; */ /* // self.iconView.transform = CGAffineTransformMakeRotation(M_PI_4); // self.iconView.layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 9998);// X, Y, Z // [self.iconView.layer setValue:@(M_PI_2) forKeyPath:@"transform.rotation.z"]; */ // self.iconView.transform = CGAffineTransformMakeScale(0.5, 0.5); self.iconView.layer.transform = CATransform3DMakeScale(1 , 1, 998); } - (void)test2 { self.iconView.layer.borderWidth = 10; self.iconView.layer.borderColor = [UIColor purpleColor].CGColor; self.iconView.layer.cornerRadius = 10; // 设置超出主图层的部分剪切掉 // self.customView.clipsToBounds = YES; self.iconView.layer.masksToBounds = YES; self.iconView.layer.bounds = CGRectMake(0, 0, 200, 200); self.iconView.layer.position = CGPointMake(100 , 100); } - (void)test { /**/ // 设置layer边框 self.customView.layer.borderWidth = 10; // 设置layer边框颜色 self.customView.layer.borderColor =[UIColor blackColor].CGColor; // 设置layer的圆角(设置主图层的圆角) self.customView.layer.cornerRadius = 10; // 设置超出主图层的部分剪切掉 // self.customView.clipsToBounds = YES; // self.customView.layer.masksToBounds = YES; // 设置的image不是展示在主图层上的, 是展示在子图层上的 self.customView.layer.contents = (id)[UIImage imageNamed:@"me"].CGImage; // 设置阴影颜色 self.customView.layer.shadowColor = [UIColor purpleColor].CGColor; // 设置阴影偏移位 // 如果为正数, 代表往右边偏移 self.customView.layer.shadowOffset = CGSizeMake(10, 10); // 设置阴影透明的 0~1 1完全不透明 0 完全透明 self.customView.layer.shadowOpacity = 1;} @end
----------
自定义Layer
// // NJViewController.m // #import "NJViewController.h" #import <QuartzCore/QuartzCore.h> @interface NJViewController () @end @implementation NJViewController - (void)viewDidLoad { [super viewDidLoad]; // 如果一个控制是另外一个控件的子控件, 那么这个控件中的layer也是另外一个控件的子layer // NSLog(@"star - %@", self.view.layer.sublayers); CALayer *layer = [CALayer layer]; layer.backgroundColor = [UIColor redColor].CGColor; layer.bounds = CGRectMake(0, 0, 100, 100); // layer.position = CGPointMake(200, 200); // layer.contents = (id)[UIImage imageNamed:@"me"].CGImage; [self.view.layer addSublayer:layer]; } - (void)test { NSLog(@"star - %@", self.view.layer.sublayers); // 1.创建layer // CALayer *layer = [[CALayer alloc] init]; CALayer *layer = [CALayer layer]; layer.backgroundColor = [UIColor redColor].CGColor; layer.bounds = CGRectMake(0, 0, 100, 100); layer.position = CGPointMake(200, 200); layer.borderWidth = 10; layer.cornerRadius = 10; // 将layer添加在界面上 [self.view.layer addSublayer:layer]; // NSLog(@"%@", layer.superlayer); // 获取layer的父视图 NSLog(@"end - %@", self.view.layer.sublayers); // // UIView *view = [[UIView alloc] init]; // view.superview; // view.subviews; // [self.view addSubview:view]; } @end