HYBControllerTransitions是自定義圍場動畫API封裝類庫,使用簡便。使用者不須要了解太多轉場動畫知識,便可輕鬆接入項目使用。spring
這是一個給開發者們提供自定義push、pop、dismiss和present轉場動畫的開源組件。如今庫中支持泡泡放大縮小圍場、模態半屏轉場和移動切換轉場(KeyNote某轉場效果)。對於開發者們來講,這是一個很不錯的開源庫。開發者不須要了解轉場知識,只須要簡單一個API就能夠實現對應的功能。閉包
若是想要更深刻地學習,請在源碼中查看對應的註釋說明!less
在設計此類庫時,但願以最簡潔的API來實現轉場功能。若是不懂任何轉場知識的人,也能輕鬆使用,那纔算達到目標。ide
所以,若是您沒有學習過相關轉場方面的知識,請不要擔憂,這個類庫不須要您掌握太多轉場的知識,只須要懂基本的OC語法便可輕鬆接入項目使用。函數
公共的屬性及API都封裝在HYBBaseTransition類型中,它遵照了須要實現轉場的全部協議並實現之。若是目前類庫中所提供的效果不知足您的需求,您能夠直接繼承於HYBBaseTransition於實現您但願獲得的效果。佈局
默認轉場動畫使用的不是彈簧動畫效果,若是以爲彈簧動畫效果更佳,僅須要設置爲YES便可。學習
1
2
3
4
5
6
7
|
/**
* Default is NO, if set to YES, it will be presented and dismissed with
* spring animation.
*/
@property (nonatomic, assign) BOOL animatedWithSpring;
|
固然,使用到彈簧天然須要設置其參數。不過做者都提供有默認值,都是通過調試過獲得的值。若是以爲默認值所獲得的效果不夠好,請自行調整參數:動畫
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* The initial Spring velocity, Only when animatedWithSpring is YES, it will take effect.
* Default is 1.0 / 0.5. If you don't know, just use the default value.
*/
@property (nonatomic, assign) CGFloat initialSpringVelocity;
/**
* The Spring damp, Only when animatedWithSpring is YES, it will take effect.
*
* Default is 0.5. If you don't know, just use the default value.
*/
@property (nonatomic, assign) CGFloat damp;
|
目前所支持的動畫效果有如下:this
present時,以泡泡圓形放大;dismiss時,以泡泡圓形縮小至起點。當須要實現此轉場效果時,請使用HYBBubbleTransition類型。效果圖以下:atom
使用起來很是簡單,只須要一個API且只在一處調用便可。好比,如今有HYBModalBubbleViewController,它有一個點擊事件,在點擊後會回調onPresent函數,而後配置以下便可實現轉場:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
- (void)onPresent {
HYBBubbleFromBottomController *vc = [[HYBBubbleFromBottomController alloc] init];
vc.modalPresentationStyle = UIModalPresentationCustom;
// Remember to own it strongly
// Because delegate is weak reference, and it will be released after out of the function body.
self.bubbleTransition = [[HYBBubbleTransition alloc] initWithPresented:^(UIViewController *presented, UIViewController *presenting, UIViewController *source, HYBBaseTransition *transition) {
// You need to cast type to the real subclass type.
HYBBubbleTransition *bubble = (HYBBubbleTransition *)transition;
// If you want to use Spring animation, set to YES.
// Default is NO.
// bubble.animatedWithSpring = YES;
bubble.bubbleColor = presented.view.backgroundColor;
// 因爲一個控制器有導航,一個沒有,致使會有64的偏差,因此要記得處理這種狀況
CGPoint center = [self.view viewWithTag:1010].center;
center.y += 64;
bubble.bubbleStartPoint = center;
} dismissed:^(UIViewController *dismissed, HYBBaseTransition *transition) {
// Do nothing and it is ok here.
// If you really want to do something, here you can set the mode.
// But inside the super class, it is set to be automally.
// So you do this has no meaning.
transition.transitionMode = kHYBTransitionDismiss;
}];
vc.transitioningDelegate = self.bubbleTransition;
[self presentViewController:vc animated:YES completion:NULL];
}
|
這裏會present HYBBubbleFromBottomController這個控制器類,可是HYBBubbleFromBottomController對象什麼也不須要作,就能夠直接實現了轉場。
是否是真的很簡單呢?
若是想要了解更多功能功能,請在源代碼中查看類中所提供的全部公開屬性,幾乎都有默認值,若不須要修改,直接使用默認值就能夠了。
注意事項:必定要將代理設置爲對應的轉場類對象。並且必定要在當前控制器強引用該轉場類對象,由於設置爲轉場代理,只是弱使用,若是沒有強引用,它就會被釋放掉:
1
2
3
4
|
// 代理再也不是設置爲self,而是設置爲轉場對象
vc.transitioningDelegate = self.bubbleTransition;
|
當須要實現半屏呈現且帶縮放效果的轉場動畫時,可使用此HYBModalTransition類。它僅支持present/dismiss模式。它提供了更多的屬性設置,稍候講解。效果以下:
使用很是簡單。假設在HYBModalHalfController有一個點擊事件,點擊後會回調onPresent函數,只須要在此函數中實現便可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
- (void)onPresent {
HYBModalHalfDetailController *vc = [[HYBModalHalfDetailController alloc] init];
self.transition = [[HYBModalTransition alloc] initWithPresented:^(UIViewController *presented, UIViewController *presenting, UIViewController *source, HYBBaseTransition *transition) {
HYBModalTransition *modal = (HYBModalTransition *)transition;
modal.scale = (CGPoint){0.95, 0.95};
// If you don't specify, it will use default value
// modal.presentedHeight = 350.0;
// If you don't want to, set to YES or do no set.
modal.shouldDismissOnTap = YES;
// Default is NO, if set to YES, it will use spring animation.
modal.animatedWithSpring = YES;
// Default is YES. including navigation bar when take snapshots.
// When has navigation bar, if set to NO, it looks not so good.
// modal.scapshotIncludingNavigationBar = NO;
} dismissed:^(UIViewController *dismissed, HYBBaseTransition *transition) {
// do nothing
// 註釋掉也沒有關係,內部已經自動設置了。
transition.transitionMode = kHYBTransitionDismiss;
}];
vc.transitioningDelegate = self.transition;
[self presentViewController:vc animated:YES completion:NULL];
}
|
對於HYBModalHalfDetailController控制器類,什麼也不須要操做。這是否是有點太過於簡單了?對於dismissed這個block,其實這裏徹底能夠設置爲nil,由於不須要作任何操做,默認就自動設置了mode。
若是當前控制器類有導航,最好仍是連導航條也一塊兒生成截圖,這樣效果會好不少。默認就是YES。若是不但願如此,請手動設置爲N:
1
2
3
4
5
6
7
|
/**
* Whether to include navigation bar when take snapshots.
* Default is YES. If NO, it has only the presenting view.
*/
@property (nonatomic, assign) BOOL scapshotIncludingNavigationBar;
|
在彈出來以後,默認是添加了點擊手勢,能夠自動dismiss。默認爲YES,若是不但願如此,請手動設置爲NO:
1
2
3
4
5
6
7
8
|
/**
* When tap on the presenting view, should it automatically is dismissed.
*
* Default is YES.
*/
@property (nonatomic, assign) BOOL shouldDismissOnTap;
|
都提供了默認值,可是若是想要調整到一個讓您滿意的效果,也許這些屬性就能夠幫助您實現:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/**
* Make the from view scale to the specified scale.
*
* Default is (0.9, 0.9)
*/
@property (nonatomic, assign) CGPoint scale;
/**
* The height for destination view to present.
*
* Default is half of destination view, it means desView.frame.size.height / 2
*/
@property (nonatomic, assign) CGFloat presentedHeight;
|
相似於KeyNote的神奇移動效果push、pop動畫。效果圖以下:
使用起來也是很簡單的,不一樣於present/dismiss模式,因爲push的時候還不能獲得轉場過去的目標視圖,好比效果圖中的切換過去後看到的圖片控件。在建立控制器類的時候,控件是不存在的,所以只能使用其它的辦法來實現。
這裏所採用的方案是經過給UIViewController添加擴展屬性,如此就能夠在建立UI的地方,將目標控件賦值。擴展所添加的屬性爲:
1
2
3
4
5
6
7
|
/**
* Set a target view to show. When push, it will transition to
* the target view. and when poped, it will pop from the target view.
*/
@property (nonatomic, strong, nonnull) UIView *hyb_toTargetView;
|
假設當前控制器類爲HYBMoveViewController,它有一個collectionview,呈風格佈局顯示一個圖片列表。當點擊cell的時候,就切換(push)到HYBMoveDetailController控制器中顯示更多內容。
實現以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
HYBMoveDetailController *vc = [[HYBMoveDetailController alloc] init];
HYBGridModel *model = self.datasource[indexPath.item];
vc.image = model.clipedImage;
self.transition = [[HYBMoveTransition alloc] initWithPushed:^(UIViewController *fromVC, UIViewController *toVC, HYBBaseTransition *transition) {
HYBMoveTransition *move = (HYBMoveTransition *)transition;
HYBGridCell *cell = (HYBGridCell *)[collectionView cellForItemAtIndexPath:indexPath];
move.targetClickedView = cell.imageView;
move.animatedWithSpring = YES;
} poped:^(UIViewController *fromVC, UIViewController *toVC, HYBBaseTransition *transition) {
// Do nothing, unless you really need to.
}];
self.navigationController.delegate = self.transition;
[self.navigationController pushViewController:vc animated:YES];
}
|
在點擊的時候,須要建立一個轉場對象,而後咱們將導航類的代理設置爲這個轉場對象,以下:
1
2
3
|
self.navigationController.delegate = self.transition;
|
這樣就能夠實現內部自動處理了。當push的時候,會回調pushed閉包,在這裏返回了轉場對象,須要提供相關屬性設置,才能實現。必定要傳targetClickedView屬性,這是被點擊的控件,也是用於切換效果用的。
注意:若是導航的代理原來是其它類對象持有,在poded閉包中應該要設置成原來的導航代理。
在HYBMoveDetailController控制器中,在viewDidLoad這裏建立UI的地方,建立了一個圖片控件,而後如些設置:
1
2
3
4
|
// You must specify a target view with this.
self.hyb_toTargetView = imgView;
|
OK,到此就實現好功能了!
是否足夠簡單?
支持pod,可直接使用pod添加如下代碼到Podfile中:
1
2
3
|
pod 'HYBControllerTransitions', '~> 1.0.0'
|
若是您的工程不支持Pod,呆直接將HYBControllerTransitions目錄放到您的工程中便可!
使用pod來安裝