iOS笔记15

1
//判断点在不在按钮上
if (CGrectContainsPoint(btn.frame, point));

2
//判断两个类是否为同一个类 class
for (MyBezierPath *path in self.pathArray)
{
if ([path class] == [UIImage class] )
{
……………….
}
}

3
//layoutSubviews
- (void)layoutSubviews
{
[super layoutSubviews];

}

4
//CAlayer的属性
宽度和高度
@property CGRect bounds;

位置(默认指中点,具体由anchorPoint决定)
@property CGPoint position;

锚点(x,y的范围都是0-1),决定了position的含义
@property CGPoint anchorPoint;

背景颜色(CGColorRef类型)
@property CGColorRef backgroundColor;

形变属性
@property CATransform3D transform;

边框颜色(CGColorRef类型)
@property CGColorRef borderColor;

边框宽度
@property CGFloat borderWidth;

圆角半径
@property CGColorRef borderColor;

内容(比如设置为图片CGImageRef)
@property(retain) id contents;

5
//UIImageView layer与 UIView layer的不同地方(UIImageView设置圆角要超出层以外的东西要裁剪掉)
//设置圆角
//对所的layer设置的属性,只作用在根层上.
self.imageV.layer.cornerRadius = 50;

//让Veiw超过根层以外的东西,都给裁剪掉
self.imageV.layer.masksToBounds = YES;

6
//self.imageV.layer.transform 、layer的 CATransform3D属性
//3d.旋转的时候才有3d效果
//angle:旋转的角度
//x,y,z轴
[UIView animateWithDuration:0.5 animations:^{

    //旋转
    //        self.imageV.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0);
    //平移
    //        self.imageV.layer.transform = CATransform3DMakeTranslation(10, 50, 200);

    //        self.imageV.layer.transform = CATransform3DMakeScale(0.5, 0.5, 0);

    //把结构体转成对象
    //通过KVC来做的形变
    //        NSValue *value =  [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)];
    //        [self.imageV.layer setValue:value forKeyPath:@"transform"];
    //什么时候用KVC,当我们做一些快速旋转,平移,缩放的时候用KVC
    [self.imageV.layer setValue:@(0.5) forKeyPath:@"transform.scale.x"];

}];

7
//NSValue 将一个非oc对象包装成oc对象
NSValue *value = [NSValue valueWithCATransform3D:CATransform3DTranslate(self.imageView.layer.transform, 30, 0, 0)];
[self.imageView.layer setValue:value forKeyPath:@”transform”];

8
//position和anchorPoint
CALayer有2个非常重要的属性:position和anchorPoint

@property CGPoint position;
用来设置CALayer在父层中的位置
以父层的左上角为原点(0, 0)

@property CGPoint anchorPoint;
称为“定位点”、“锚点”
决定着CALayer身上的哪个点会在position属性所指的位置
以自己的左上角为原点(0, 0)
它的x、y取值范围都是0~1,默认值为(0.5, 0.5)

9
//隐式动画(非Root Layer) 隐式动画的关闭 、动画事务 CATransaction
每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层)

所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动画

什么是隐式动画?
当对非Root Layer的部分属性进行修改时,默认会自动产生一些动画效果
而这些属性称为Animatable Properties(可动画属性)

列举几个常见的Animatable Properties:
bounds:用于设置CALayer的宽度和高度。修改这个属性会产生缩放动画
backgroundColor:用于设置CALayer的背景色。修改这个属性会产生背景色的渐变动画
position:用于设置CALayer的位置。修改这个属性会产生平移动画
//经过测试,self.layer.transform = CATransform3DRotate(self.layer.transform, M_PI, 1, 0, 0); 也有隐式动画


可以通过动画事务(CATransaction)关闭默认的隐式动画效果
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.myview.layer.position = CGPointMake(10, 10);
[CATransaction commit];


//更具体的代码
- (void)viewDidLoad {
    [super viewDidLoad];
    //只有非根层才有隐式动画.
    CALayer *layer = [CALayer layer];
    layer.frame = CGRectMake(50, 50, 100, 100);
    layer.backgroundColor = [UIColor redColor].CGColor;
    self.layer = layer;
    [self.view.layer addSublayer:layer];


}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //系统默认会自动产生一些动画效果,我们叫它是隐式动画.

    //想要取消隐式动画
    //包装成一个事务.
    //有很多操作绑定在一起, 当这些操作全部执行完毕时, 才继续执行下面的任务.这个就是事务.
    //开启事务.
    [CATransaction begin];

    //取消动画操作.
    [CATransaction setDisableActions:NO];
    //设置隐式动画执行的时长
    [CATransaction setAnimationDuration:1];

    self.layer.bounds = CGRectMake(0, 0, arc4random_uniform(100), arc4random_uniform(200));
    self.layer.backgroundColor = [self randomColors].CGColor;
    self.layer.position = CGPointMake(arc4random_uniform(300), arc4random_uniform(300));
    self.layer.cornerRadius = arc4random_uniform(50);

    //提交事务.
    [CATransaction commit];

}

10
//CALayer 的transform的属性列表
rotation.x
rotation.y
rotation.z
rotation

scale.x
scale.y
scale.z
scale

translation.x
translation.y
translation.z
translation

11
//创建一个CALayer、CALayer显示一张图片(layer.contents)
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(50, 50, 100, 100);
layer.backgroundColor = [UIColor redColor].CGColor;

layer.contents = (id)[UIImage imageNamed:@"阿狸头像"].CGImage;
[self.view.layer addSublayer:layer];

12
//UIView当中的center就是layer当中的position点.

NSLog(@"%@",NSStringFromCGPoint(self.view.center));
NSLog(@"%@",NSStringFromCGPoint(self.view.layer.position));

self.view.center = CGPointMake(100, 120);
NSLog(@"%@",NSStringFromCGPoint(self.view.center));
NSLog(@"%@",NSStringFromCGPoint(self.view.layer.position));

self.view.layer.position = CGPointMake(200, 200);
NSLog(@"%@",NSStringFromCGPoint(self.view.center));
NSLog(@"%@",NSStringFromCGPoint(self.view.layer.position));

//输出结果
2015-09-28 22:06:28.446 CALayer[2084:19467] {187.5, 333.5}
2015-09-28 22:06:28.447 CALayer[2084:19467] {187.5, 333.5}
2015-09-28 22:06:28.447 CALayer[2084:19467] {100, 120}
2015-09-28 22:06:28.447 CALayer[2084:19467] {100, 120}
2015-09-28 22:06:28.447 CALayer[2084:19467] {200, 200}
2015-09-28 22:06:28.447 CALayer[2084:19467] {200, 200}

14
//CAlayer属性的创建用weak,可以理解成
[self.view.layer addSublayer:layer]; //有个强指针,用weak即可

15
//CATransform3D的旋转方向
旋转的方向,它是根据旋转角度的大小来的.它会找它到这个角的最短路径.如果发现两个路径都相同,它默认是逆时针旋转.

16
//获取系统的时间 NSCalendar、NSDateComponents

//获取当前是多少秒.
NSCalendar *calendar = [NSCalendar currentCalendar];
//获取日历当中哪些组件.
NSDateComponents *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
//当前秒数.
NSInteger curSec = cmp.second;

//当前是多少分
NSInteger curMin = cmp.minute;

//当前是多少小时
NSInteger curHour = cmp.hour;

17
//CABasicAnimation基础核心动画 的使用
//创建动画对象
CABasicAnimation *anim = [CABasicAnimation animation];
//设置属性值.
anim.keyPath = @”transform.rotation”;
anim.toValue = @(M_PI);

//动画完成时会自动的删除动画.
//动画完成时要不要删除动画
anim.removedOnCompletion = NO;

//让动画始终保存最前面的效果.
anim.fillMode = @"forwards";   //kCAFillModeForwards  注:和上面的一行代码缺一不可

//添加动画
[self.redView.layer addAnimation:anim forKey:nil];

18
//心跳效果、CABasicAnimation(repeatCount、duration、autoreverses反转)
//添加动画
CABasicAnimation *anim = [CABasicAnimation animation];
//设置属性值.
anim.keyPath = @”transform.scale”;
anim.toValue = @0;

//想要动画执行多次
anim.repeatCount = MAXFLOAT;

//设置动画的执行时长
anim.duration = 0.5;

//反转,怎么去的,怎么样回来.
anim.autoreverses = YES;

//添加动画
[self.imageV.layer addAnimation:anim forKey:nil];

19
//图片抖动(帧动画) CAKeyframeAnimation

//添加抖动的动画.
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
//设置属性值.
anim.keyPath = @"transform.rotation";
anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5))];
anim.autoreverses = YES;
//动画一直重复执行.
anim.repeatCount = MAXFLOAT;
anim.duration = 0.5;
//添加动画
[self.iconView.layer addAnimation:anim forKey:nil];



//以上的anim.autoreverses = YES; 可以不写,
//将anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5))];改成如下
anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5)),@(angle2Rad(-5))];

20
// CAKeyframeAnimation绕所画的path路径运动(anim.path = path.CGPath)

//添加抖动的动画.
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
//设置属性值.
anim.keyPath = @"position";

//根据路径做动画
//    UIBezierPath *path = [UIBezierPath bezierPath];
//    [path moveToPoint:CGPointMake(50, 50)];
//    [path addLineToPoint:CGPointMake(200, 400)];

UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 300, 300)];

anim.path = path.CGPath;

//anim.autoreverses = YES;
//动画一直重复执行.
anim.repeatCount = MAXFLOAT;
anim.duration = 0.5;
//添加动画
[self.iconView.layer addAnimation:anim forKey:nil];

21
//CATransition 转场动画

//转场代码必须得要和转场动画在同一个方法当中.,并没有要求他们的上下顺序.
//转场代码.
_i++;
if (_i > 3) {
    _i = 1;
}
NSString *imageName = [NSString stringWithFormat:@"%d",_i];
self.imageV.image = [UIImage imageNamed:imageName];

//添加转场动画
CATransition *anim = [CATransition animation];

anim.duration = 1;
//动画从哪个点开始.
anim.startProgress = 0.3;
//动画从哪个点结束.
anim.endProgress = 0.5;

//转场类型.
anim.type = @"pageCurl";

[self.imageV.layer addAnimation:anim forKey:nil];

23
//UIView来做转场动画
[UIView transitionWithView:self.imageV duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
_i++;
if (_i > 3) {
_i = 1;
}
NSString *imageName = [NSString stringWithFormat:@”%d”,_i];
self.imageV.image = [UIImage imageNamed:imageName];
} completion:nil];

24
//UIView转场动画类型
UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,

25
//动画组 CAAnimationGroup
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
//animations数组当中存入的都是animation对象
//平移
CABasicAnimation *anim = [CABasicAnimation animation];
anim.keyPath = @”position.y”;
anim.toValue = @(400);
//缩放
CABasicAnimation *anim2 = [CABasicAnimation animation];
anim2.keyPath = @”transform.scale”;
anim2.toValue = @(0.5);
//会自动执行数组当中所有的动画对象.
animGroup.animations = @[anim,anim2];
//动画完成不要删除动画
animGroup.removedOnCompletion = NO;
//始终保持最前面的效果
animGroup.fillMode = kCAFillModeForwards;
[self.redView.layer addAnimation:animGroup forKey:nil];

26
//UIView动画与核心动画的区别 、UIView动画和核心动画的使用场合
//UIView动画与核心动画的区别
1.核心动画只作用在CALayer上面.
2.核心动画并不会修改属性的真实值.我们看的到一切都是假象

//什么时候使用UIView动画,什么时候使用核心动画.
    1.当与用户进行交互的时候使用UIView动画

//什么时候使用核心动画最多.
    做转场动画.
    组动画.
    帧动画的时候.

27
//拿像素与当前屏幕坐标点的比例
CGFloat scale = [UIScreen mainScreen].scale;

28
//截取图片 (从一张图片中截取所要的矩形部分) CGImageRef –> UIImage
UIImage *oriNorImage = [UIImage imageNamed:@”LuckyAstrology”];
//image:要截取原始图片.
//rect:截取图片的范围
imageX = i * imageW;
CGRect rect = CGRectMake(imageX, imageY, imageW, imageH);
CGImageRef clipnNorImage = CGImageCreateWithImageInRect(oriNorImage.CGImage, rect);

//把CGImage转成UIImage
UIImage *newClipNorImage =  [UIImage imageWithCGImage:clipnNorImage];

29
//图片截取效果测试 (像素与当前屏幕坐标点的比例的使用)
//情况1 由于@2x 的原因,imageView显示只有原图的1/4
UIImage *image = [UIImage imageNamed:@”LuckyRotateWheel”];
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
self.imageView.image = newImage;

//情况2根据scale imageView显示的是完整的图片
UIImage *image = [UIImage imageNamed:@"LuckyRotateWheel"];
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat imageW = image.size.width * scale;
CGFloat imageH = image.size.height *scale;
CGRect rect = CGRectMake(0, 0, imageW, imageH);
CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
self.imageView.image = newImage;

30
//自定义按钮 取消按钮的选中高亮、从写setHighlighted: 这个方法
- (void)setHighlighted:(BOOL)highlighted
{

}

31
//CABasicAnimation 的代理无需遵守协议 、非正式的协议、 id delegate ,而非id<协议> delegate
CABasicAnimation *anim = [CABasicAnimation animation];
anim.delegate = self;

//总共就连个代理方法
//当动画完成时调用.
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{}
//当动画开始的时候调用
- (void)animationDidStart:(CAAnimation *)anim{}

32
//根据transform计算出旋转的角度 atan2f(transform.b, transform.a)
CGFloat angle = atan2f(transform.b, transform.a);

若绕360度  angle的值变化为:0--180:   0---->3.14
                        180--360: -3.14---->0

33
//CGAffineTransformMakeRotation、CGAffineTransformRotate 的理解
//可以理解成参照的角度为 0
self.contentView.transform = CGAffineTransformMakeRotation(-angle);
//可以理解成参照的角度为 自己的transform
self.contentView.transform = CGAffineTransformRotate(self.contentView.transform, -angle);

34
//父控件transform的改变不会影响到子控件transform的改变 <自己>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值