GeekBand iOS開發(fā)高級(jí)進(jìn)階學(xué)習(xí)筆記(第二周)

動(dòng)畫效果

動(dòng)畫的原理來(lái)自視覺(jué)的殘留效應(yīng),視覺(jué)神經(jīng)反應(yīng)速度約16分之1秒,只要畫面變化速度快于視覺(jué)神經(jīng)反應(yīng)速度就會(huì)感覺(jué)是連續(xù)的,理論上10FPS(10幀每秒)就感覺(jué)動(dòng)畫流暢,16FPS就完全平滑。電影膠片達(dá)到了24FPS每秒,計(jì)算機(jī)顯示要60FPS才能讓人感覺(jué)平滑。
合理的使用動(dòng)畫效果可以是用戶更加容易理解和使用軟件,在relationship, structure, cause & effect這幾個(gè)場(chǎng)景里是非常適合用動(dòng)畫的。

UIView提供的動(dòng)畫支持
(UIViewAnimation):depricated 已經(jīng)過(guò)時(shí)iOS4之后就不再用了,遇到老的代碼維護(hù)再回頭看

UIView(UIViewAnimationWithBlocks)

+(void)animateWithDuration:(NSTimeInterval) delay:(NSTimeIntercal) options:(UIViewAnimationOptions) animations:(void (^)(void)) completion:(void (^__nullable)(BOOL finished));
+(void)animateWithDuration:(NSTimeInterval) animations:(void (^)(void)) completion:(void (^__nullable)(BOOL finished))
+(void)animateWithDuration:(NSTimeInterval) animations:(void (^)(void))

UIView動(dòng)畫的屬性
frame
bounds
center
transform
alpha
backgroundColor
contentStretch

UIView的Keyframe動(dòng)畫支持
(UIViewKeyframeAnimations)

+(void)animateKeyframesWithDuration:(NSTimeInterval) delay:(NSTimeInterval) options:(UIViewKeyframeAnimationOptions) animations:(void (^)(void)) completion:(void (^__nullable)(BOOL finished))
+(void)animateKeyframesWithRelativeStartTime:(double) relativeDuration:(double) animations:(void (^)(void))  
//startTime和duration都是比例值,從0-1.0

Spring Animation
從iOS7開始系統(tǒng)開始全面使用SprintAnimation

+(void)animationWithDuration:(NSTimeInterval)duration   //總時(shí)長(zhǎng),一個(gè)浮點(diǎn)數(shù),以秒為單位
                       delay:(NSTimeInterval)delay      //延時(shí)啟動(dòng),一個(gè)浮點(diǎn)數(shù),以秒為單位
      usingSpringWithDamping:(CGFloat)dampingRatio      //阻尼值0-1.0
        initalSpringVelocity:(CGFloat)velocity          //初速度
                     options:(UIViewAnimationOptions)options
                  animations:(void (^)(void))animations
                   completion:(void (^)(BOOL finished))completion

Autolayout環(huán)境下的動(dòng)畫
修改constraint
-[view setNeedsUpdateConstraints]
-[view layoutIfNeeded] in animation block

View Transition

View切換動(dòng)畫:用動(dòng)畫過(guò)程提示已經(jīng)切換到新界面叫View Transition。
在iOS中切換動(dòng)畫分為兩類
一類是修改子視圖顯示 small changes
UIView的類方法:
+transitionWithView:duration:options:animations:completion:
WithView:參數(shù)是上級(jí)View
duration:指定動(dòng)畫的長(zhǎng)度
options:指定用那種類型的動(dòng)畫,可選參數(shù)如下

UIViewAnimationOptionTransitionNone
UIViewAnimationOptionTransitionFlipFormLeft
UIViewAnimationOptionTransitionFlipFromRight
UIViewAnimationOptionTransitionCurlUp
UIViewAnimationOptionTransitionCurlDown
UIViewAnimationOptionTransitionCrossDissolve
UIViewAnimationOptionTransitionFlipFromTop
UIViewAnimationOptionTransitionFlipFromBottom
UIViewAnimationOptionsAllowAnimatedContent //為了性能考慮,默認(rèn)時(shí)動(dòng)畫效果是subview的一個(gè)snapshot(照相圖片)來(lái)完成的,所以只能整個(gè)view動(dòng),如果view內(nèi)部也需要有動(dòng)畫效果,就必須添加這個(gè)參數(shù),允許Subview內(nèi)部也有動(dòng)畫效果。

animations:一個(gè)block參數(shù),做subview動(dòng)畫的管理
completion:一個(gè)block參數(shù),可安排一些代碼在動(dòng)畫完成后工作。

子視圖類(subview)的[show, hide, add, remove]這四種情況可以用動(dòng)畫來(lái)顯示

還有一類是視圖替換:從一個(gè)場(chǎng)景(fromView)切換到另一個(gè)場(chǎng)景(toView)
類方法有:
+transitionFromView:toView:duration:options:completion:
默認(rèn)的轉(zhuǎn)換場(chǎng)景是將一個(gè)View刪除,再增加一個(gè)View
也可以將兩個(gè)View都添加到一個(gè)父View中,用options參數(shù)傳入U(xiǎn)IViewAnimationOptionShowHideTranstionViews這樣就不會(huì)刪除,直接顯示一個(gè)View,隱藏另外一個(gè)View。性能更好,但是由于要同時(shí)加載兩個(gè)subview內(nèi)存開銷較大。

Core Animation

Core Animation是iOS與OS X平臺(tái)上負(fù)責(zé)圖形渲染與動(dòng)畫的基礎(chǔ)設(shè)施。Core Animation可以動(dòng)畫視圖和其他的可視元素。Core Animation為你完成了實(shí)現(xiàn)動(dòng)畫所需的大部分繪幀工作。你只需在配置少量的動(dòng)畫參數(shù)(如開始點(diǎn)位置和結(jié)束點(diǎn)位置)就可啟動(dòng)Core Animation。Core Animation將大部分實(shí)際的繪圖任務(wù)交給了圖形硬件處理,圖形硬件會(huì)加速圖形渲染的速度。這種自動(dòng)化的圖形加速讓動(dòng)畫具有更高的幀率且更加平滑,但這并不會(huì)增加CPU的負(fù)擔(dān)而導(dǎo)致影響你應(yīng)用的運(yùn)行速度。
Core Animation是整套API,通過(guò)操作Layer來(lái)形成動(dòng)畫效果。
對(duì)Layer屬性的修改都在CATransaction里,可以用CATransaction +begin來(lái)顯示的開始一個(gè)事務(wù)。之后用CATransaction setValue:forKey來(lái)處理事務(wù),可進(jìn)行一下操作:
kCATransactionAnimationDuration;
kCATransactionDisableActions;
kCATransactionAnimationTimingFunction;
kCATransactionCompletionBlock;
處理完畢后可以用 CATransaction +commit來(lái)確認(rèn)操作的修改,CATransaction操作還可以互相嵌套。

隱式動(dòng)畫(Implicit Animation)
只要更改了Layer的可動(dòng)畫屬性,就會(huì)自動(dòng)觸發(fā)動(dòng)畫效果,可動(dòng)畫屬性如下:

layer可動(dòng)畫屬性

當(dāng)我們修改屬性時(shí),Layer就會(huì)調(diào)用自身delegate中的-actionForLayer:forKey的方法,這個(gè)方法會(huì)查找對(duì)應(yīng)key的CAAction和style屬性,如果都沒(méi)有的話會(huì)調(diào)用layer的-defaultActionForKey方法。
如果需要禁止隱式動(dòng)畫,只要讓-actionForKey:找不到CAAction就可以了,有兩種實(shí)現(xiàn)方法:1、在delegate中重寫一個(gè)-actionForKey:方法,并且將返回值設(shè)為nil。2、調(diào)用[CATransaction setDisableActions:YES];將動(dòng)畫效果關(guān)閉。

顯示動(dòng)畫
隱式動(dòng)畫是系統(tǒng)提供的我們修改不了,如果要自己設(shè)置動(dòng)畫效果要用到顯示動(dòng)畫CAAnimation。
CAAnimation實(shí)現(xiàn)了CAAction和CAMediaTiming兩個(gè)接口

顯示動(dòng)畫

Basic Animation
CAAnimation(以及CAAnimation的子類),全部都是顯式動(dòng)畫,這樣動(dòng)畫播放后,表現(xiàn)層回恢復(fù)到模型層的原始狀態(tài),這就意味著,如果動(dòng)畫播放完后,會(huì)恢復(fù)到原來(lái)的樣子,所以在動(dòng)畫播放完后要對(duì)模型層進(jìn)行修改,例如:self.view.layer.backgroundColor=[UIColor blueColor].CGColor;
CABasicAnimation +animationWithKeyPath:animatableProperty
config animation
formValue:如果沒(méi)有設(shè)置,則使用當(dāng)前值
toValue
byValue
duration
repeatCount = HUGE_VALF; 將動(dòng)畫設(shè)置為無(wú)限循環(huán)
layer -addAnimation:animation forKey:animationName
set layer:property to final value:CAAnimation(以及CAAnimation的子類),全部都是顯式動(dòng)畫,這樣動(dòng)畫播放后,表現(xiàn)層回恢復(fù)到模型層的原始狀態(tài),這就意味著,如果動(dòng)畫播放完后,會(huì)恢復(fù)到原來(lái)的樣子,所以在動(dòng)畫播放完后要對(duì)模型層進(jìn)行修改,例如:
self.view.layer.backgroundColor=[UIColor blueColor].CGColor;

Keyframe Animation
CFKeyframeAnimation +animationWithKeyPath:animatablePropertyName-
path是一個(gè) CGPathRef 對(duì)象,默認(rèn)是空的,當(dāng)我們創(chuàng)建好CAKeyframeAnimation的實(shí)例的時(shí)候,可以通過(guò)制定一個(gè)自己定義的path來(lái)讓 某一個(gè)物體按照這個(gè)路徑進(jìn)行動(dòng)畫。這個(gè)值默認(rèn)是nil 當(dāng)其被設(shè)定的時(shí)候 values 這個(gè)屬性就被覆蓋 。
values是一個(gè)數(shù)組,提供了一組關(guān)鍵幀的值, 當(dāng)使用path的 時(shí)候 values的值自動(dòng)被忽略。

-(void)animationOfCAKeyframeAnimationPath  
{  
    //初始化一個(gè)View,用來(lái)顯示動(dòng)畫  
    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];  
    redView.backgroundColor=[UIColor redColor];  
      
    [self.view addSubview:redView];  
      
    CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];  
    //初始化路徑  
    CGMutablePathRef aPath=CGPathCreateMutable();  
    //動(dòng)畫起始點(diǎn)  
    CGPathMoveToPoint(aPath, nil, 20, 20);  
    CGPathAddCurveToPoint(aPath, nil,   
                          160, 30,//控制點(diǎn)  
                          220, 220,//控制點(diǎn)   
                          240, 380);//控制點(diǎn)  
      
    ani.path=aPath;  
    ani.duration=10;  
    //設(shè)置為漸出  
    ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];  
    //自動(dòng)旋轉(zhuǎn)方向  
    ani.rotationMode=@"auto";  
      
    [redView.layer addAnimation:ani forKey:@"position"];  
}  

Group Animation
CAAnimationGroup +animation
agroup.animations<NSArray*>
group.duration 整個(gè)組的時(shí)間,如果在其組內(nèi)的動(dòng)畫時(shí)長(zhǎng)大于整組的時(shí)間,那么整組時(shí)間到之后會(huì)強(qiáng)制中斷動(dòng)畫
layer -addAnimation:group:forKey:

Timing
每一個(gè)layer都有自己的time space,在彼此之間做換算可以用

-convertTime:fromLayer:
-convertTime:toLayer:
//如果設(shè)置為nil就是和系統(tǒng)時(shí)間做比較

所有的動(dòng)畫都實(shí)現(xiàn)了CAMediaTiming協(xié)議:

CAMediaTiming protocol

CACurrentMediaTime() 提供了系統(tǒng)的時(shí)間returns current system time \ mach_absolute_time() to seconds
animation.beginTime 設(shè)置了動(dòng)畫開始的時(shí)間,如果希望動(dòng)畫不是立即開始的話。

網(wǎng)絡(luò)編程 - 原生API
TCP/IP簡(jiǎn)介

TCP/IP

推薦一個(gè)TCP/IP的介紹 http://blog.csdn.net/goodboy1881/article/category/204448

HTTP簡(jiǎn)介

HTTP

REST
是一種服務(wù)端API設(shè)計(jì)模式,使用Path指示資源,HTTP Verb表達(dá)操作。
API方法冪等(Idempotence)的概念:在設(shè)計(jì),購(gòu)買或支付等請(qǐng)求的時(shí)候,有時(shí)因?yàn)榫W(wǎng)絡(luò)原因客戶可能會(huì)多次發(fā)送同一個(gè)請(qǐng)求,但是只需執(zhí)行一次就可以了,執(zhí)行多次與執(zhí)行一次效果一樣,這就叫做冪等。

iOS的網(wǎng)絡(luò)支持
Foundation (NSURL* classes) 日常使用較多
CFNetwork (core foundation 的網(wǎng)絡(luò)設(shè)置,在做比較低層的網(wǎng)絡(luò)控制時(shí)需要使用)
BSD Socket 最底層的網(wǎng)絡(luò),支持Unix的網(wǎng)絡(luò)操作,純C語(yǔ)言編寫的網(wǎng)絡(luò)操作

在iOS7以后我們都使用NSURLSession

//NSURLSession 讀取URL
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:myURL
  completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                 {...}
]resume];

NSURLSession的結(jié)構(gòu)


NSURLSession的結(jié)構(gòu)

NSURLSession的基本用法
根據(jù)Session類型選擇NSURLSessionConfiguration
設(shè)置Configuration,如指定NDURLCache等
用Configuration創(chuàng)建Session

+[NSURLSession sessionWithConfiguration:delegate:delegateQueue:] 
+[NSURLSession sessionWithConfiguration:] //簡(jiǎn)單版本

在session里創(chuàng)建網(wǎng)絡(luò)訪問(wèn)任務(wù)
啟動(dòng)任務(wù)
-[NSURLSessionTask resume]
NSURLSession分為三種類型
1、Default sessions類型,用全局配置基于一個(gè)公共的文件緩存系統(tǒng),把文件緩存到該文件中,用戶的憑證保存在Keychain中。此方法最為常用
2、Ephemeral sessions類型,用私有的配置,文件全部緩存在內(nèi)存之中,數(shù)據(jù)僅在Session期間存在。
3、Background sessions類型,另有專門的進(jìn)程處理數(shù)據(jù)傳輸,其他跟Default一樣。一般給后臺(tái)應(yīng)用程序使用。

Task也有三種類型
Data Task:用NSData收發(fā)數(shù)據(jù),適合臨時(shí)與服務(wù)端通信,用-[session dataTaskWithURL:]方法創(chuàng)建
Download Task:接受文件,支持后臺(tái)下載,用-[session downloadTaskWithURL:]方法創(chuàng)建
Upload Task:發(fā)送文件,支持后臺(tái)上傳,用-[session uploadTaskWithURL:]方法創(chuàng)建

網(wǎng)絡(luò)連接狀態(tài)
SystemConfiguration.framework
SCNetworkReachabilityRef to host/sockaddr
SCNetworkReachabilityGetFlags()
-kSCNetworkReachabilityFlags*
SCNetworkReachabilitySetCallback()
-監(jiān)聽網(wǎng)絡(luò)連接變化情況

AFNetworking

AF的全稱是Alamo Fire
官方連接:https://github.com/AFNetworking/AFNetworking
目前最新版本為:3.04 (2015.4.24)
安裝:可以使用CocoPods安裝(CocoPods是一款Xcode系統(tǒng)插件管理軟件)
CocoPods的安裝方法:http://blog.csdn.net/showhilllee/article/details/38398119
下面用一個(gè)實(shí)例來(lái)演示一下AFNetworking的基本用法:

 // 請(qǐng)求的參數(shù)
 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"20131129", @"date", @"1", @"startRecord", @"5", @"len", @"1234567890", @"udid", @"Iphone", @"terminalType", @"213", @"cid", nil];
 // 初始化Manager
 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 
 // 不加上這句話,會(huì)報(bào)“Request failed: unacceptable content-type: text/plain”錯(cuò)誤,因?yàn)槲覀円@取text/plain類型數(shù)據(jù)
 manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// Get請(qǐng)求
 [manager GET:@"發(fā)送請(qǐng)求的HTTP地址" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
 // 這里可以獲取到目前的數(shù)據(jù)請(qǐng)求的進(jìn)度
 } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
 // 請(qǐng)求成功,解析數(shù)據(jù)
 NSLog(@"%@", responseObject);
 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
 
 NSLog(@"%@", dic);
 } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
 // 請(qǐng)求失敗
 NSLog(@"%@", [error localizedDescription]);
 }];
Get Resource
Upload

本周作業(yè)的說(shuō)明
通過(guò)地圖Api(高德,百度或是谷歌),對(duì)當(dāng)前位置的經(jīng)緯度(可以寫死,非實(shí)際定位)進(jìn)行網(wǎng)絡(luò)通信,并解析數(shù)據(jù)得到當(dāng)前地址。不推薦使用與地圖有關(guān)的第三方庫(kù)。

百度的API說(shuō)明:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
首先要去注冊(cè)一個(gè)百度的密鑰:http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5
注冊(cè)一個(gè)瀏覽器端密鑰就可以了,如果注冊(cè)iOS端秘鑰,獲取信息的url要記得在密鑰之后加上自己的安全碼,即Bundle Identifier的名字。
例如:http://api.map.baidu.com/geocoder/v2/?address=北京市海淀區(qū)上地十街10號(hào)&output=json&ak=E4805d16520de693a3fe707cdc962045&mcode=BundleIdentifier&callback=showLocation

手機(jī)自動(dòng)定位功能:
首先,我們需要在工程中導(dǎo)入CoreLocation系統(tǒng)框架。然后在我們的控制器中引入頭文件。

#import <CoreLocation/CoreLocation.h>

然后,聲明一個(gè)CLLocationManager對(duì)象作為成員變量,用于定位獲取經(jīng)緯度坐標(biāo),并遵守協(xié)議

@interface TYViewController () <CLLocationManagerDelegate> {
    CLLocationManager *_locationManager;
}

開始定位

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initializeLocationService];
    // Do any additional setup after loading the view.
}
- (void)initializeLocationService {
    // 初始化定位管理器
    _locationManager = [[CLLocationManager alloc] init];
    // 設(shè)置代理
    _locationManager.delegate = self;
    // 設(shè)置定位精確度到米
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    // 設(shè)置過(guò)濾器為無(wú)
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    // 開始定位
    [_locationManager startUpdatingLocation];
}

定位參考鏈接:http://www.2cto.com/kf/201402/279078.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容