iOS基礎知識匯總(一)

來源:互聯網
上載者:User

標籤:

一、通知

1.監聽通知

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

當anObject對象發布一條名字叫做aName的通知時,就會調用observer的aSelector方法

 

2.發布通知

// 發布一個通知對象(name、object、userInfo)

- (void)postNotification:(NSNotification *)notification;

 

// anObject發布了一個名字叫做aName的通知

- (void)postNotificationName:(NSString *)aName object:(id)anObject;

 

// anObject發布了一個名字叫做aName的通知,並且傳遞了一個額外資料:aUserInfo

- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

 

二、從storyboard裡面載入控制器

1.載入storyboard

1> 方法1:利用storyboard檔案的名字

+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil;

 

2> 方法2:控制器的storyboard方法

- [UIViewController storyboard]

 

2.載入控制器

// 返回第一個控制器(初始化控制器,箭頭所指的控制器)

- (id)instantiateInitialViewController;

// 返回唯一標識是identifier的控制器

- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier;

 

三、block的定義格式

1.block變數的定義

void (^myblock)(int, int);

myblock = ^(int a, int b) {

 

};

 

int age = 10;

 

2.@property的定義

@property (nonatomic, copy) void (^myblock)(int, int);

@property (nonatomic, assign) int age;

 

3.當做方法的參數

- (void)setMyBlock:(void (^)(int, int))myblock;

- (void)setAge:(int)age;

 

四、CGRect常見函數

1.CGRectGetMinX(CGRect rect) 等函數

 

2.CGRectContainsPoint(<#CGRect rect#>, <#CGPoint point#>)

 

五、UIImage的裁剪

CGImageCreateWithImageInRect(<#CGImageRef image#>, <#CGRect rect#>)

 

六、消除鋸齒

- (void)clearAlias

{

    self.layer.borderWidth = 2;

    self.layer.borderColor = [UIColor clearColor].CGColor;

    // 就會把圖層當做是一個bitmap來渲染

    self.layer.shouldRasterize = YES;

    

    for (UIView *child in self.subviews) {

        [child clearAlias];

    }

}

 

七、計時器

1.NSTimer

1> 人為控制重新整理頻率

2> 對重新整理速度要求不高,適合慢重新整理

3> 建立timer

// 返回一個新的timer,但是不會開始計時,需要調用fire方法

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

// 返回一個新的timer,會馬上開始計時

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

4> 停止計時器(只要計時器停止了,以後不能再次開始計時)

- (void)invalidate;

 

2.CADisplayLink

1> 包含QuartzCore架構

2> 固定重新整理頻率(1秒鐘重新整理60次)

3> 對重新整理速度要求高,適合快重新整理

4> 建立displaylink

// 返回一個CADisplayLink計時器對象,1秒內會調用60次target的sel方法,並且將CADisplayLink當做參數傳入

+ (CADisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel;

5> 開始計時

- (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSString *)mode;

6> 停止計時

- (void)invalidate;

7> 刷幀間隔

@property(readonly, nonatomic) CFTimeInterval duration;

8> 控制暫停或者繼續

@property(getter=isPaused, nonatomic) BOOL paused;

 

八、動畫

1.UIView封裝的動畫

1> 首尾式

[UIView beginAnimations:nil context:nil];

// ... 需要執行怎樣的動畫

[UIView commitAnimations];

 

2> block

[UIView animateWithDuration:0.5 animations:^{

    // 需要執行的動畫

} completion:^(BOOL finished) {

    // 動畫完成

}];

 

3> 轉場動畫(過渡動畫)

// 讓某個view執行轉場動畫

[UIView transitionWithView:<#(UIView *)#> duration:<#(NSTimeInterval)#> options:<#(UIViewAnimationOptions)#> animations:<#^(void)animations#> completion:<#^(BOOL finished)completion#>];

 

2.CALayer的動畫

// CABasicAnimation和CAKeyframeAnimation的keyPath可以是哪些值?

// 在xcode文檔搜尋:CALayer Animatable Properties

// transform的具體屬性:搜尋catransform3d key path

1> CABasicAnimation

* fromValue  初始值 

* toValue  最終值  (從初始化變化到最後某一個值)

* byValue  步進值  (在初始值的基礎上,增加多少值)

 

2> CAKeyframeAnimation

* values

 

3> CATransition(轉場動畫)

CATransition *anim = [CATransition animation];

anim.type = @"cube";

anim.subtype = kCATransitionFromBottom;

[view.layer addAnimation:anim forKey:nil];

 

4> CAAnimationGroup

* 動畫,可以同時執行多個動畫

 

3.如何選擇動畫

1> 如果需要重複執行多次動畫,最好選擇CALayer動畫

2> 如果動畫執行完畢後,就要用到前面的一些東西,最好選擇UIView的block動畫

3> 如果需要同時執行多個動畫,最好選擇CAAnimationGroup

4> UIView動畫和CALayer動畫中最靈活的是CALayer的動畫

 

4.自訂一些動畫

用CADisplayLink,在刷幀方法完成需要執行的動畫

iOS基礎知識匯總(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.