开头
小结
- 整理的有点乱,这些知识点挺散的,我目前的项目用到的地方不是很多
- 事件那里实现有点麻烦,而且我感觉目前没必要整理,有空再说QAQ
CALayer
实际用处
基础用法
UIView *redView = [UIView new]
redView.frame = CGRectMake(100, 100, 100, 100)
redView.backgroundColor = [UIColor redColor]
//设置边框
redView.layer.borderWidth = 10
//边框颜色
redView.layer.borderColor = [UIColor grayColor].CGColor
//阴影
redView.layer.shadowOffset = CGSizeMake(4, 4)
redView.layer.shadowColor = [UIColor blueColor].CGColor
redView.layer.shadowRadius = 10
redView.layer.shadowOpacity = 1
//圆角
redView.layer.cornerRadius = 20
//超出layer的范围不显示,超出范围的阴影也会被裁掉
redView.layer.masksToBounds = YES
//bounds 大小
redView.layer.bounds = CGRectMake(0, 0, 200, 200)
//position 默认指的是center
redView.layer.position = CGPointMake(0, 0)
//设置内容 __bridge id
redView.layer.contents = (__bridge id)([UIImage imageNamed:@"Gin_1"].CGImage)
[self.view addSubview:redView]
self.layer.transform = CATransform3DMakeRotation(M_PI_4, 1, 0, 0);
self.layer.transform = CATransform3DRotate(self.layer.transform, M_PI_4, 0, 0, 1);
self.layer.transform = CATransform3DScale(self.layer.transform, 0.5, 0.5, 0.1);
self.layer.transform = CATransform3DTranslate(self.layer.transform, 10, 0, 10);
隐式动画
CALayer *layer = [CALayer new]
layer.backgroundColor = [UIColor redColor].CGColor
layer.position = CGPointMake(200, 200)
layer.bounds = CGRectMake(0, 0, 100, 100)
layer.contents = (__bridge id)[UIImage imageNamed:@"Gin_1"].CGImage
//添加
[self.view.layer addSublayer:layer]
- CALayer自带隐式动画,也就是说,当改变控件layer时,会有一定的变化动,自动产生动画效果
- 而根控件layer指的是,直接从创建的控件获取的layer是没有隐式动画的
//获取触摸对象
UITouch *t = touches.anyObject
CGPoint p = [t locationInView:t.view]
//自带隐式动画 根控件没有隐式动画
self.layer.bounds = CGRectMake(0, 0, 200, 200)
self.layer.opacity = 0.5
self.layer.position = p
//获取触摸对象
UITouch *t = touches.anyObject
CGPoint p = [t locationInView:t.view]
//自带隐式动画 根控件没有隐式动画
self.layer.bounds = CGRectMake(0, 0, 200, 200)
self.layer.opacity = 0.5
//禁用隐式动画
[CATransaction begin]
[CATransaction setDisableActions:YES]
self.layer.position = p
[CATransaction commit]
核心动画
实际用处
基础用法
基本动画
//1.创建动画
CABasicAnimation* anim = [CABasicAnimation new]
//2.做什么动画
anim.keyPath = @"position.x"
// anim.fromValue = @(10)
// anim.toValue = @(300)
//累加
// anim.byValue = @(10)
//开始时间
anim.beginTime = CACurrentMediaTime() + 3
//起始值
anim.fromValue = @300
//结束值
anim.toValue = @400
anim.duration = 3
/**
kCAFillModeForwards //动画开始3秒后,从初始位置跳到x=300,移动到400
kCAFillModeBackwards //动画开始后,直接跳到动画开始的位置,如上面跳到x=300处,3秒后到500,并结束后回到最初的位置
kCAFillModeBoth //包括上面两个
kCAFillModeRemoved 默认回到初始位置
*/
//默认会回到原来位置,不回到原来的位置
anim.fillMode = kCAFillModeForwards
anim.removedOnCompletion = NO
//3.添加动画(谁做动画)
[self.layer addAnimation:anim forKey:nil]
关键帧动画
//关键帧动画
CAKeyframeAnimation *anim = [CAKeyframeAnimation new]
anim.keyPath = @"position"
NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)]
NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(150, 100)]
NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(100, 150)]
NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(150, 150)]
//关键帧数据
anim.values = @[v1,v2,v3,v4]
//路径动画
// UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:1]
// anim.path = path.CGPath
//时间
anim.duration = 3
//重复次数
anim.repeatCount = INT_MAX
[self.layer addAnimation:anim forKey:nil]
组动画
//组动画
CAAnimationGroup *group = [CAAnimationGroup new]
//基本动画 自己旋转
CABasicAnimation *anim = [CABasicAnimation new]
anim.keyPath = @"transform.rotation"
anim.byValue = @(2 * M_PI * 10)
//关键帧动画
CAKeyframeAnimation *anim1 = [CAKeyframeAnimation new]
anim1.keyPath = @"position"
//路径动画
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:1]
anim1.path = path.CGPath
group.animations = @[anim, anim1]
group.duration = 3
group.repeatCount = INT_MAX
[self.layer addAnimation:group forKey:nil]
转场动画
- view的转场
- 需要设置动画方式和方向
- 下面是配合手势实现的,手势也在下面整理
- (IBAction)imageChange:(UISwipeGestureRecognizer *)sender {
NSLog(@"UISwipeGestureRecognizer")
CATransition *anim = [CATransition new]
//动画方式
//官方方式
//`fade', `moveIn', `push' and `reveal'. Defaults to `fade'
//私有,有些有效果有些没效果
//@"cube"、@"suckEffect"、@"oglFlip"、@"rippleEffect"、@"pageCurl"、@"pageUnCurl"、@"cameraIrisHollowOpen"、@"cameraIrisHollowClose"
anim.type = @"push"
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
self.imageName --
if (self.imageName == 0) {
self.imageName = 6
}
//方向
anim.subtype = kCATransitionFromLeft
NSLog(@"UISwipeGestureRecognizerDirectionRight")
}
else if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
self.imageName ++
if (self.imageName == 7) {
self.imageName = 1
}
anim.subtype = kCATransitionFromRight
NSLog(@"UISwipeGestureRecognizerDirectionLeft")
}
self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld", self.imageName]]
//转场动画
anim.duration = 1
[self.imageView.layer addAnimation:anim forKey:nil]
}
- (void)viewDidLoad {
[super viewDidLoad]
self.imageName = 1
}
手势识别
实现步骤
- 创建手势对象
- 对某一个view添加手势
- 实现手势方法
敲击
- (void)tap:(UITapGestureRecognizer *)sender
{
NSLog(@"UITapGestureRecognizer");
}
/**
@property (nonatomic) NSUInteger numberOfTapsRequired
@property (nonatomic) NSUInteger numberOfTouchesRequired API_UNAVAILABLE(tvos)
*/
//创建手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]
tap.numberOfTapsRequired = 2
tap.numberOfTouchesRequired = 2
//对某一个view添加手势
[self.imageView addGestureRecognizer:tap]
长按
-(void)longPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"UILongPressGestureRecognizer");
}
}
/**
@property (nonatomic) NSTimeInterval minimumPressDuration
@property (nonatomic) CGFloat allowableMovement
@property(nonatomic,readonly) UIGestureRecognizerState state
*/
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]
[self.imageView addGestureRecognizer:longPress]
longPress.minimumPressDuration = 1
longPress.allowableMovement = 100
轻扫
-(void)swipe:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"UISwipeGestureRecognizer --- right");
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"UISwipeGestureRecognizer --- left");
}
}
/**
@property(nonatomic) UISwipeGestureRecognizerDirection direction
*/
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]
[self.imageView addGestureRecognizer:swipe]
UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)]
swipe1.direction = UISwipeGestureRecognizerDirectionLeft
[self.imageView addGestureRecognizer:swipe1]
旋转
-(void)rotation:(UIRotationGestureRecognizer *)sender
{
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, sender.rotation);
sender.rotation = 0;
NSLog(@"UIRotationGestureRecognizer");
}
/**
@property (nonatomic) CGFloat rotation
*/
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]
[self.imageView addGestureRecognizer:rotation]
捏合
-(void)pinch:(UIPinchGestureRecognizer *)sender
{
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, sender.scale, sender.scale);
sender.scale = 1;
NSLog(@"UIPinchGestureRecognizer");
}
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]
[self.imageView addGestureRecognizer:pinch]
拖拽
-(void)pan:(UIPanGestureRecognizer *)sender
{
CGPoint p = [sender translationInView:sender.view];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, p.x, p.y);
[sender setTranslation:CGPointZero inView:sender.view];
NSLog(@"UIPanGestureRecognizer");
NSLog(@"%.f", sender.view.frame.origin.x);
}
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]
[self.imageView addGestureRecognizer:pan]
多个手势同时添加
- 通过
UIGestureRecognizerDelegate代理方法实现
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]
tap.numberOfTapsRequired = 1
tap.numberOfTouchesRequired = 2
tap.delegate = self
[self.imageView addGestureRecognizer:tap]
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]
[self.imageView addGestureRecognizer:rotation]
rotation.delegate = self
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]
[self.imageView addGestureRecognizer:pinch]
pinch.delegate = self
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
事件
- 第一响应者,表示当前用户与该对象进行交互
- 响应者链条,如果第一响应者不处理,会向上传递,找到父视图,以此类推,直到顶层视图,最后达到UIWindow和UIApplication,如果整个过程都没有响应,该事件就会被丢弃
事件类型
UIEventTypeTouches,
UIEventTypeMotion, 摇晃
UIEventTypeRemoteControl, 遥控事件
UIEventTypePresses , 按压
UIEventTypeScroll, 滚动
UIEventTypeHover, 手指停留在上面
UIEventTypeTransform,
- UIEventSubtype 子事件类型,主要针对遥控事件进行细分
// available in iPhone OS 3.0
UIEventSubtypeNone = 0,
// for UIEventTypeMotion, available in iPhone OS 3.0
UIEventSubtypeMotionShake = 1,
// for UIEventTypeRemoteControl, available in iOS 4.0
UIEventSubtypeRemoteControlPlay = 100,
UIEventSubtypeRemoteControlPause = 101,
UIEventSubtypeRemoteControlStop = 102,
UIEventSubtypeRemoteControlTogglePlayPause = 103,
UIEventSubtypeRemoteControlNextTrack = 104,
UIEventSubtypeRemoteControlPreviousTrack = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
UIEventSubtypeRemoteControlEndSeekingForward = 109,
触摸事件
touches参数
window 触摸所在的窗口
view 触摸的视图
tapCount 短时间内点击的次数
timestamp 触摸事件变化的时间,时间戳形式(秒)
phase 触摸阶段
UITouchPhaseBegan, 0
UITouchPhaseMoved, 1
UITouchPhaseStationary, 2 手指接触但是没有移动
UITouchPhaseEnded, 3
UITouchPhaseCancelled, 4
touches获取坐标
UITouch *t = touches.anyObject;
CGPoint p = [t locationInView:self.superview];
CGPoint p1 = [t locationInView:self];
NSLog(@"%@", NSStringFromCGPoint(p));
NSLog(@"%@", NSStringFromCGPoint(p1));
UITouch *t = touches.anyObject
CGPoint p = [t previousLocationInView:self]
触摸事件状态
- cancelled,当触摸被取消(比如触摸过程中被来电打断)
- 其他三个如字面意思
- (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 {
}
摇晃事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event;
- 实现,通过Device->shake模拟
- view中
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
self.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
NSLog(@"UIEventSubtypeMotionShake");
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad]
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]
[self.myView becomeFirstResponder]
}
- (void)viewDidDisappear:(BOOL)animated {
[self.myView resignFirstResponder]
}