登录 用户中心() [退出] 后台管理 注册
   
您的位置: 首页 >> CLQ工作室开源代码 - [函数库] >> 主题: [ios/functions_c.m]ViewRotateAni_v2 旋转一个 uiview 及动画的一些重要概念     [回主站]     [分站链接]
标题
[ios/functions_c.m]ViewRotateAni_v2 旋转一个 uiview 及动画的一些重要概念
clq
浏览(393) + 2021-03-02 15:57:08 发表 编辑

关键字:

[2021-03-02 21:43:07 最后更新]
//更精确的控制版本,如果是不断旋转最好是用这个//ani_second 是持续的秒数
//speed_fun_line 很重要,控制动画速度是否是线性的,默认开头和结束慢,中间快,这里应该要改成线性的 -- 速度始终一致
void ViewRotateAni_v2(bool speed_fun_line, UIView * view, int angle_360, dispatch_block_t block, NSTimeInterval ani_second)
{
//旋转动画//这些都是不能累加的,要累加的话,要自己记住上次的值
//[UIView animateWithDuration:2.0f animations:^{
// _btn2.transform = CGAffineTransformMakeRotation(M_PI);
//
//} completion:^(BOOL finished) {
// //没用//_btn2.transform = CGAffineTransformMakeRotation(M_PI);
//
//}];

//旋转动画//这些都是不能累加的,要累加的话,要自己记住上次的值
if (false == speed_fun_line)
[UIView animateWithDuration:ani_second animations:^{
view.transform = CGAffineTransformMakeRotation((CGFloat)angle_360 * M_PI / 180.0);

} completion:^(BOOL finished) {
//没用//_btn2.transform = CGAffineTransformMakeRotation(M_PI);

block();

}];

//默认的动画是非线性的,开头和结束慢,中间快,这里应该要改成线性的 -- 速度始终一致

//UIViewAnimationOptionCurveLinear 就是线性速度
//参考 https://www.jianshu.com/p/2670735bb2ca

if (true == speed_fun_line)
[UIView animateWithDuration:ani_second
delay:0
options:UIViewAnimationOptionCurveLinear //options
animations:^{
view.transform = CGAffineTransformMakeRotation((CGFloat)angle_360 * M_PI / 180.0);

}
completion:^(BOOL finished) {
//没用//_btn2.transform = CGAffineTransformMakeRotation(M_PI);
block();
}

];



}//

clq
2021-03-02 16:42:36 发表 编辑


应用示例.


//动画旋转
-(void)StartAni:(double) speed_second
{
//[self startAnimation]; return;
//startAnimation(self.imgAni); return;
//开线程吧,在主线程 sleep 是不会画出东西的
//dispatch_queue_t globalQueue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

go(^{

//int istop = 0; //不加 __block 标志会报错的
__block int istop = 0;

for (; ; )
{

//sleep_c(1 * 1000);
//sleep_c(1); //释放 cpu

//上次的还没结束就应该等待
if (1 == istop) {
//sleep_c(1);
// continue;
}//

istop = 1;

go_main(^{
//ShowMessage(@"aaa");

//旋转动画//这些都是不能累加的,要累加的话,要自己记住上次的值
////ViewRotateAni(self.imgAni, 180);
////ViewRotateAni(self.imgAni, 360); //奇怪,不能直接旋转成 360 度,359 都不行
////return; //其实用上面的组合可以得到正确的效果,但理论上解释不清楚

if (1 == istop) {
//sleep_c(1);
//continue;
// return;
}//

istop = 1;


//更精确的旋转//先旋转到 180 再旋转到 360, 最后修改标志位,重复进行
ViewRotateAni_v2(true, self.imgAni, 180, ^{

ViewRotateAni_v2(true, self.imgAni, 360, ^{ istop = 0; }, speed_second / 2);//2.0f); //不行,换线性动画函数后就可以
//ViewRotateAni_v2(self.imgAni, 360, ^{ istop = 0; [self StartAni]; }, 2.0f); //奇怪,这样也不行


//这样做出来的动画,会在结束时有个停顿的效果,原因是 ios 的动画默认是开始结束慢,中间快的非线性函数模式
//所以目前来说暂时只能这样叠加
//ViewRotateAni_v2(self.imgAni, 360, ^{ }, 2.0f);


}, speed_second / 2);//2.0f);

}); //gomain

//sleep_c(1 * 1000);
//sleep_c(1); //这样 cpu 仍然,比较高.性能最好的是,直接等待动画完成后再进行. 当然可以考虑递归.
sleep_c(speed_second * 1000);

}//for


});//go


}//


明显比后面的性能好很多.

//这个的效果不错,但 cpu 占用有点高 iphone6s 会有 %8 不太适合大范围应用
double angle = 0;
void startAnimation(UIImageView * imageView)
{
//__block double angle;// = 0; //放到里面来加上 __block 关键字也可以 //不行
//UIImageView * imageView = self.imgAni;

CGAffineTransform endAngle = CGAffineTransformMakeRotation(angle * (M_PI / 180.0f));

[UIView

//应该是执行时间
animateWithDuration: 0.02 //0.03 //0.02 //0.01 //这个参数大于等于 0.03 的话就明显的卡了

//延时多少秒后再执行,一般设置为 0
delay: 0 //10

options:UIViewAnimationOptionCurveLinear

//动画的内容
animations:^{

imageView.transform = endAngle;

}

//结束后要执行的东西
completion:^(BOOL finished) {

angle += 10;
//[self startAnimation];
startAnimation(imageView);

}

];

}//

clq
2021-03-02 16:43:30 发表 编辑

另外参考路径动画.
---------------------------------------------------------------

多个UIBezierPath拼接起来的运动轨迹,在执行动画的时候,动画出现卡顿,

要实现动画的所设置的路径代码:

- (void)drawRect:(CGRect)rect {

UIColor *color = [UIColor redColor];
[color set];
_aPath = [UIBezierPath bezierPath];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y-50) radius:50 startAngle:M_PI_2 endAngle:M_PI*2.f*3/4 clockwise:YES];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y) radius:100 startAngle:-M_PI_2 endAngle:M_PI*2.f*3/4 clockwise:YES];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y) radius:100 startAngle:-M_PI_2 endAngle:M_PI*2.f*3/4/3 clockwise:YES];
[_aPath addArcWithCenter:CGPointMake(self.center.x, self.center.y+50) radius:50 startAngle:M_PI_2 endAngle:M_PI*2.f*3/4 clockwise:YES];

_aPath.lineWidth = 5.0;
_aPath.lineCapStyle = kCGLineCapRound; //线条拐角
_aPath.lineJoinStyle = kCGLineCapRound; //终点处理
[_aPath stroke];
}

下面的是我执行动画代码:

CAKeyframeAnimation *keyframeAnimation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
keyframeAnimation.path = dr.aPath.CGPath;
keyframeAnimation.repeatCount=1;
keyframeAnimation.removedOnCompletion = NO;
keyframeAnimation.fillMode = kCAFillModeForwards;
keyframeAnimation.duration = 4.0f;
keyframeAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
keyframeAnimation.delegate=self;
[imageView.layer addAnimation:keyframeAnimation forKey:nil];

clq
2021-03-02 21:41:41 发表 编辑

[图片]

https://www.jianshu.com/p/2670735bb2ca 处的图示非常有意思,可以让我们参考实现类似于 ios 的动画效果。











clq
2021-03-02 21:42:13 发表 编辑

[图片]

2

clq
2021-03-02 21:42:35 发表 编辑

[图片]

3

clq
2021-03-02 21:43:07 发表 编辑

[图片]

4


总数:6 页次:1/1 首页 尾页  
总数:6 页次:1/1 首页 尾页  


所在合集/目录



发表评论:
文本/html模式切换 插入图片 文本/html模式切换


附件:



NEWBT官方QQ群1: 276678893
可求档连环画,漫画;询问文本处理大师等软件使用技巧;求档softhub软件下载及使用技巧.
但不可"开车",严禁国家敏感话题,不可求档涉及版权的文档软件.
验证问题说明申请入群原因即可.

Copyright © 2005-2020 clq, All Rights Reserved
版权所有
桂ICP备15002303号-1