搜尋
首頁web前端html教學iOS中Animation 动画 UI_22_html/css_WEB-ITnose

1.iOS中我们能看到的控件都是UIView的子类,比如UIButton UILabel UITextField UIImageView等等

2.UIView能够在屏幕的显示是因为在创建它的时候内部自动添加一个CALayer图层,通过这个图层在屏幕上显示的时候会调用一个drawRect: 的方法,完成绘图,才能在屏幕上显示

3.CALayer 本身就具有显示功能,但是它不能响应用户的交互事件,如果只是单纯的显示一个图形,此时你可以使用CALayer创建或者是使用UIView创建,但是如果这个图形想响应用户交互事件,必须使用UIView或者子类

动画知识框图如下:


#import "ViewController.h"#import "UITextField+Shake.h"@interface ViewController ()@property (retain, nonatomic) IBOutlet UIImageView *balloon;@property (retain, nonatomic) IBOutlet UITextField *TF;@property (retain, nonatomic) IBOutlet UIButton *bounces;@property (retain, nonatomic) IBOutlet UIView *animationView;@property (retain, nonatomic) IBOutlet UIImageView *cloud;@end<span style="font-family: 'STHeiti Light';">@implementation ViewController</span>
- (void)viewDidLoad {    [super viewDidLoad];    //取到当前视图控制器自带view的图层    CALayer *layer = self.view.layer;//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];//    button.layer //button的图层    //layer 的color必须是CGColor    self.animationView.layer.backgroundColor = [UIColor greenColor].CGColor;}

//给TF创建一个类目:

UITextField+Shake.h#import <uikit>@interface UITextField (Shake)- (void)shake;@endUITextField+Shake.m#import "UITextField+Shake.h"@implementation UITextField (Shake)//震动的方法- (void)shake{    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];    keyFrame.values = @[@(self.center.x + 10),@(self.center.x),@(self.center.x - 10)];    keyFrame.repeatCount = 10;    keyFrame.duration = 0.03;    [self.layer addAnimation:keyFrame forKey:nil];  }@end</uikit>


开始动画按钮点击事件

- (IBAction)handleAnimation:(UIButton *)sender {    //UIView的属性动画    [self handlePropertyAnimation];     //UIView的属性动画 Block形式    [self handlePrepertyAnimationBlock];       //UIView的过渡动画    [self handleTrabsitionAnimation];        //CALayer动画    [self handleCALayer];        //CALayer 的基础动画    [self handleBasicAnimation];    //CALayer的关键帧动画    [self handleKeyFrameAnimation];    //UITextField 调用输入震动框方法    [self.TF shake];        //CALayer的过渡动画    [self handleLayerCATransactionAnimation];    //CAAinmationGroup 分组动画    [self handleAnimatonGroup];    }

//UIView的属性动画 可动画的属性 : frame center bounds alpha backgroundColor transfrom

//修改属性做动画,动画结束后属性修改的结果是真实的作用到动画的视图上,不能恢复到之前的样子

- (void)handlePropertyAnimation{    //iOS4.0之前必须把要修改的可动画属性写在begin 和 commit 之间    //开始动画    [UIView beginAnimations:nil context:nil];    //指定代理 动画的代理不需要遵循协议,因为此代理就没有制定协议    [UIView setAnimationDelegate:self];    //设置动画的持续时间    [UIView setAnimationDuration:3.0];    //设置动画的重复次数 给重复效果旋转效果立即消失    [UIView setAnimationRepeatCount:3.0];    //设置动画的反转效果    [UIView setAnimationRepeatAutoreverses:YES];    //设置动画的变化速度    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    //如果要实现这个方法必须设置代理,此方法在动画结束后触发    [UIView setAnimationDidStopSelector:@selector(makeAnimationBack)];        //修改属性做动画    //1.center 修改中心点    CGPoint center = self.animationView.center;    center.y += 10;    self.animationView.center =center;    //2.修改透明度 alpha    self.animationView.alpha = 0.0;    //3.变形 tranform    // 之前形变量    //旋转的角度180/4    self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, M_PI_4);        self.animationView.transform = CGAffineTransformScale(self.animationView.transform, 0.5, 0.5);        //提交动画    [UIView commitAnimations];}

//恢复到视图之前的状态

- (void)makeAnimationBack{    //        self.animationView.center = self.view.center;        self.animationView.alpha = 1.0;    //恢复到tranform最初状态,最初状态就在CGAffineTransformIdentity记录    self.animationView.transform = CGAffineTransformIdentity;    }//UIView的属性动画 Block形式- (void)handlePrepertyAnimationBlock{    //iOS4.0之后使用block的形式做动画            __block typeof(self)weakSelf = self;    //1.block 的第一种形式    //01.动画的持续时间//    [UIView animateWithDuration:2 animations:^{//        //1.修改中心点//        CGPoint center = weakSelf.animationView.center;//        center.y += 50;//        weakSelf.animationView.center = center;//        //2.透明度//        weakSelf.animationView.alpha = 0.0;//        //3.变形//        weakSelf.animationView.transform = CGAffineTransformRotate(weakSelf.animationView.transform, M_PI_4);//}];        //2.block的第二种形式    [UIView animateWithDuration:2 animations:^{        //1.获得中心点        CGPoint center = weakSelf.animationView.center;        //改变中心点        center.y += 50;        weakSelf.animationView.center =center;        //2.透明度        weakSelf.animationView.alpha = 0.0;        //3.形变修改transform        weakSelf.animationView.transform = CGAffineTransformScale(weakSelf.animationView.transform, 0.5, 0.2);                    } completion:^(BOOL finished) {        //返回动画之前的状态        [weakSelf makeAnimationBack];    }];        //3.block的第三种形式    //01:持续时间    //02:动画执行的延迟时间    //03:设置动画的特效    //04:修好的动画属性    //05:动画执行结束后的block块    [UIView animateWithDuration:3 delay:1 options:UIViewAnimationOptionAllowAnimatedContent animations:^{        //1.获得中心点        CGPoint center = weakSelf.animationView.center;        //改变中心点        center.y += 50;        weakSelf.animationView.center =center;        //2.透明度        weakSelf.animationView.alpha = 0.0;        //3.形变修改transform        weakSelf.animationView.transform = CGAffineTransformScale(weakSelf.animationView.transform, 0.5, 0.2);            } completion:^(BOOL finished) {         //返回动画之前的状态        [weakSelf makeAnimationBack];    }];        //block的第四种形式    //参数1:动画持续时间    //参数2:动画的延迟时间    //参数3:设置弹簧的强度 范围(0.0~1.0)    //参数4:设置弹簧的速度    //参数5:动画效果    //参数6:改变动画的属性写在这里    //参数7:结束动画的时候调用的block    [UIView animateWithDuration:2 delay:1 usingSpringWithDamping:0.5 initialSpringVelocity:500 options:UIViewAnimationOptionCurveEaseInOut animations:^{        CGPoint center = weakSelf.bounces.center;        center.y += 10;        weakSelf.bounces.center = center;        //transform        weakSelf.bounces.transform = CGAffineTransformScale(weakSelf.bounces.transform, 1.2, 1.2);    } completion:^(BOOL finished) {        CGPoint center = weakSelf.bounces.center;        center.y -= 10;        weakSelf.bounces.center = center;        weakSelf.bounces.transform = CGAffineTransformIdentity;            }];}

//UIView的过渡动画

- (void)handleTrabsitionAnimation{            __block typeof(self)weakSelf = self;    //01:对哪个视图添加过渡动画    //02:动画时常    //03:动画效果    [UIView transitionWithView:self.animationView duration:2 options:UIViewAnimationOptionAllowAnimatedContent animations:^{                weakSelf.animationView.transform = CGAffineTransformRotate(weakSelf.animationView.transform, M_PI_4);            } completion:nil];    }

//CALayer动画,修改layer层的属性做动画并没有真实的作用到这个视图上,动画知识一种假象

- (void)handleCALayer{    //CALyer 动画就是对layer做动画    //边框的宽    self.animationView.layer.borderWidth = 10.0;    //边框颜色    self.animationView.layer.borderColor = [UIColor redColor].CGColor;    //切圆角//    self.animationView.layer.cornerRadius = 100;    //取出layer多余的部分//    self.animationView.layer.masksToBounds = YES;    //减掉layer多出的部分//    self.animationView.clipsToBounds = YES;    //背景图片    self.animationView.layer.contents = (id)[UIImage imageNamed:@"WDGJ785Q{`CKL4J}1{_4{(Y.jpg"].CGImage;            //视图一创建出来的时候  锚点 基准点  中心点三个点是重合的    //锚点 anchorPoint  决定layer层上的哪个点是position 锚点默认是(0.5,0.5),跟视图的中心点重合    self.animationView.layer.anchorPoint = CGPointMake(0.5, 0);    self.animationView.transform = CGAffineTransformRotate(self.animationView.transform, M_PI_4);        //基准点 Position 决定当前视图的layer,在父视图的位置,它以父视图的坐标系为准    self.animationView.layer.position = CGPointMake(160, 184);}

//CALayer 的动画基类:CAAnimation

//CABasicAnimation  基础动画

- (void)handleBasicAnimation{    //CA动画是根据KVC的原理,就修改layer的属性,以达到做动画的效果    CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"position.x"];        basic.fromValue = @(-80);    basic.toValue = @(400);    //设置动画持续的时间    basic.duration = 5.0;    //设置动画重复的次数    basic.repeatCount = 1000;    [self.cloud.layer addAnimation:basic forKey:nil];}

//CAKeyFrameAnimation 关键帧动画

- (void)handleKeyFrameAnimation{    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];    CGPoint point1 = self.cloud.center;    CGPoint point2 = CGPointMake(160, 100);    CGPoint point3 = CGPointMake(270, self.cloud.center.y);        //把一组要播放的动画需求的数值,按顺序放到数组中,此时动画执行的效果,就会按照数组中数据的顺序发生变化;    //转化point结构体类型 转化成对象类型    NSValue *value1 = [NSValue valueWithCGPoint:point1];    NSValue *value2 = [NSValue valueWithCGPoint:point2];    NSValue *value3 = [NSValue valueWithCGPoint:point3];    keyFrame.repeatCount = 1000;    keyFrame.duration = 15.0;    keyFrame.values = @[value1,value2,value3,value1];    [self.cloud.layer addAnimation:keyFrame forKey:nil];}

//CATransition CALayer 的过度动画

- (void)handleLayerCATransactionAnimation{     /*          各种动画效果  其中除了'fade', `moveIn', `push' , `reveal' ,其他属于似有的API(我是这么认为的,可以点进去看下注释).          *  ↑↑↑上面四个可以分别使用'kCATransitionFade', 'kCATransitionMoveIn', 'kCATransitionPush', 'kCATransitionReveal'来调用.          *  @"cube"                     立方体翻滚效果          *  @"moveIn"                   新视图移到旧视图上面          *  @"reveal"                   显露效果(将旧视图移开,显示下面的新视图)          *  @"fade"                     交叉淡化过渡(不支持过渡方向)             (默认为此效果)          *  @"pageCurl"                 向上翻一页          *  @"pageUnCurl"               向下翻一页          *  @"suckEffect"               收缩效果,类似系统最小化窗口时的神奇效果(不支持过渡方向)          *  @"rippleEffect"             滴水效果,(不支持过渡方向)          *  @"oglFlip"                  上下左右翻转效果          *  @"rotate"                   旋转效果          *  @"push"          *  @"cameraIrisHollowOpen"     相机镜头打开效果(不支持过渡方向)          *  @"cameraIrisHollowClose"    相机镜头关上效果(不支持过渡方向)          */        //创建过渡动画对象    CATransition *transition = [CATransition animation];    //配置动画过渡的样式    transition.type = @"cameraIrisHollowClose";    //将过渡动画添加到layer上    [self.view.layer  addAnimation:transition forKey:nil];    }

//CAAinmationGroup 分组动画

- (void)handleAnimatonGroup{    //1.创建第一个关键帧动画,给热气球一个运动轨迹    CAKeyframeAnimation *keyframePath = [CAKeyframeAnimation animationWithKeyPath:@"position"];    //贝塞尔曲线    //1.指定贝塞尔曲线的半径    CGFloat  radius = [UIScreen mainScreen].bounds.size.height / 2.0;    //01:圆心    //02:半径    //03:开始的角度    //04:结束的角度    //05:旋转方向 (YES表示顺时针 NO表示逆时针)    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, radius) radius:radius startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:YES];    //将贝塞尔曲线作为运动轨迹    keyframePath.path = path.CGPath;        //2.创建第二组关键帧动画,让热气球在运动的时候  由小--->大--->小   ;    CAKeyframeAnimation *keyFrameScale = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];    //通过一组数据修改热气球的大小    keyFrameScale.values = @[@1.0,@1.2,@1.4,@1.6,@1.8,@1.6,@1.4,@1.2,@1.0];    //创建动画分组对象    CAAnimationGroup *group = [CAAnimationGroup animation];    //将两个动画效果添加到分组动画中    group.animations = @[keyframePath,keyFrameScale];        group.duration = 8;    group.repeatCount = 1000;            [self.balloon.layer addAnimation:group forKey:nil];    }

最终效果:



版权声明:本文为博主原创文章,未经博主允许不得转载。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
什麼是自我關閉標籤?舉一個例子。什麼是自我關閉標籤?舉一個例子。Apr 27, 2025 am 12:04 AM

self-closingtagsinhtmlandxmlaretagsthatclosethem hexptneedneedingAseparateClosingTag,SightifyingmarkupStrupupStrupureAndenHancingCodingsigy.1)shemesessientInsentialInxmlforelementswithcontentsswithcontent content content content content content content content content contentcontent,確保wellwell-formedDocuments.2)Inhtmlible5,inhtmlibut forfix

超越HTML:網絡開發的基本技術超越HTML:網絡開發的基本技術Apr 26, 2025 am 12:04 AM

要構建一個功能強大且用戶體驗良好的網站,僅靠HTML是不夠的,還需要以下技術:JavaScript賦予網頁動態和交互性,通過操作DOM實現實時變化。 CSS負責網頁的樣式和佈局,提升美觀度和用戶體驗。現代框架和庫如React、Vue.js和Angular,提高開發效率和代碼組織結構。

HTML中的布爾屬性是什麼?舉一些例子。HTML中的布爾屬性是什麼?舉一些例子。Apr 25, 2025 am 12:01 AM

布爾屬性是HTML中的特殊屬性,不需要值即可激活。 1.布爾屬性通過存在與否控制元素行為,如disabled禁用輸入框。 2.它們的工作原理是瀏覽器解析時根據屬性的存在改變元素行為。 3.基本用法是直接添加屬性,高級用法可通過JavaScript動態控制。 4.常見錯誤是誤以為需要設置值,正確寫法應簡潔。 5.最佳實踐是保持代碼簡潔,合理使用布爾屬性以優化網頁性能和用戶體驗。

如何驗證您的HTML代碼?如何驗證您的HTML代碼?Apr 24, 2025 am 12:04 AM

HTML代碼可以通過在線驗證器、集成工具和自動化流程來確保其清潔度。 1)使用W3CMarkupValidationService在線驗證HTML代碼。 2)在VisualStudioCode中安裝並配置HTMLHint擴展進行實時驗證。 3)利用HTMLTidy在構建流程中自動驗證和清理HTML文件。

HTML與CSS和JavaScript:比較Web技術HTML與CSS和JavaScript:比較Web技術Apr 23, 2025 am 12:05 AM

HTML、CSS和JavaScript是構建現代網頁的核心技術:1.HTML定義網頁結構,2.CSS負責網頁外觀,3.JavaScript提供網頁動態和交互性,它們共同作用,打造出用戶體驗良好的網站。

HTML作為標記語言:其功能和目的HTML作為標記語言:其功能和目的Apr 22, 2025 am 12:02 AM

HTML的功能是定義網頁的結構和內容,其目的在於提供一種標準化的方式來展示信息。 1)HTML通過標籤和屬性組織網頁的各個部分,如標題和段落。 2)它支持內容與表現分離,提升維護效率。 3)HTML具有可擴展性,允許自定義標籤增強SEO。

HTML,CSS和JavaScript的未來:網絡開發趨勢HTML,CSS和JavaScript的未來:網絡開發趨勢Apr 19, 2025 am 12:02 AM

HTML的未來趨勢是語義化和Web組件,CSS的未來趨勢是CSS-in-JS和CSSHoudini,JavaScript的未來趨勢是WebAssembly和Serverless。 1.HTML的語義化提高可訪問性和SEO效果,Web組件提升開發效率但需注意瀏覽器兼容性。 2.CSS-in-JS增強樣式管理靈活性但可能增大文件體積,CSSHoudini允許直接操作CSS渲染。 3.WebAssembly優化瀏覽器應用性能但學習曲線陡,Serverless簡化開發但需優化冷啟動問題。

HTML:結構,CSS:樣式,JavaScript:行為HTML:結構,CSS:樣式,JavaScript:行為Apr 18, 2025 am 12:09 AM

HTML、CSS和JavaScript在Web開發中的作用分別是:1.HTML定義網頁結構,2.CSS控製網頁樣式,3.JavaScript添加動態行為。它們共同構建了現代網站的框架、美觀和交互性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),