您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

仿微信風格的首啟動引導頁與「開門」動畫

編輯:iOS 開發綜合

基於使用者體驗,多數 App 會在首次啟動時顯示幾頁簡短介紹,也就是引導頁。以下示範如何用 UIScrollView 實作左右滑動的引導頁,並在最後一頁加上一個類似微信的「開門」離場動畫。

整體結構

用 scrollView 承載多張圖片完成切換,滑到最後一張時才讓「開始使用」按鈕出現。按下按鈕後,把最後一張圖切成左右兩半,分別向左、向右平移並淡出,營造推開大門的效果,動畫結束後移除引導頁。

按鈕點擊處理

- (IBAction)startButtonDidPressed:(id)sender {
    [self.startButton setHidden:YES];

    // 把最後一張圖切成左右兩半
    NSArray *array = [UIImage splitImageIntoTwoParts:self.imageView.image];

    self.left = [[UIImageView alloc] initWithImage:[array objectAtIndex:0]];
    float height = DEVICE_IS_IPHONE5 ? 568 : 480;
    [self.left setFrame:CGRectMake(0, 0, 320, height)];

    self.right = [[UIImageView alloc] initWithImage:[array objectAtIndex:1]];
    [self.right setFrame:CGRectMake(0, 0, 320, height)];

    [self addSubview:self.left];
    [self addSubview:self.right];

    // 隱藏原本的捲動元件
    [self.pageScroll setHidden:YES];
    [self.pageControl setHidden:YES];

    self.left.transform  = CGAffineTransformIdentity;
    self.right.transform = CGAffineTransformIdentity;

    [UIView beginAnimations:@"split" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:1.2];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    [self.left setAlpha:0.15];
    [self.right setAlpha:0.15];

    self.left.transform  = CGAffineTransformMakeTranslation(-180, 0);
    self.right.transform = CGAffineTransformMakeTranslation(180, 0);

    [UIView commitAnimations];
}

動畫結束後的收尾

- (void)animationDidStop:(NSString *)animationID
                finished:(NSNumber *)finished
                 context:(void *)context {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

    if ([animationID isEqualToString:@"split"] && finished) {
        [self.left removeFromSuperview];
        [self.right removeFromSuperview];
    }
    [self removeFromSuperview];
}

關鍵在於 splitImageIntoTwoParts

整個效果的核心是把一張圖片切成兩半。這是替 UIImage 寫的分類(category),概念上不難:建立兩個寬度各為原圖一半的圖形上下文,分別把原圖的左半與右半畫進去。

@implementation UIImage (Split)

- (NSArray *)splitImageIntoTwoParts:(UIImage *)image {
    CGSize size = image.size;
    CGFloat halfWidth = size.width / 2.0;

    UIGraphicsBeginImageContext(CGSizeMake(halfWidth, size.height));
    [image drawInRect:CGRectMake(0, 0, halfWidth, size.height)];
    UIImage *leftPart = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContext(CGSizeMake(halfWidth, size.height));
    [image drawInRect:CGRectMake(-halfWidth, 0, size.width, size.height)];
    UIImage *rightPart = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return [NSArray arrayWithObjects:leftPart, rightPart, nil];
}

@end

切右半邊的技巧是把原圖往左偏移半個寬度再畫,這樣只有右半落在上下文範圍內。

幾個實務上的改進空間

beginAnimations 已過時。 上面用的 [UIView beginAnimations:] 系列在 iOS 4 以後就被 block 版本取代。改用 animateWithDuration: 不僅語意清楚,還能直接在 completion block 裡收尾,不需要另外實作 animationDidStop:

[UIView animateWithDuration:1.2 animations:^{
    self.left.alpha  = 0.15;
    self.right.alpha = 0.15;
    self.left.transform  = CGAffineTransformMakeTranslation(-180, 0);
    self.right.transform = CGAffineTransformMakeTranslation(180, 0);
} completion:^(BOOL finished) {
    [self.left removeFromSuperview];
    [self.right removeFromSuperview];
    [self removeFromSuperview];
}];

寫死尺寸要留意。 範例中的 320 × 480 / 568 是舊機型尺寸,新專案請改用 UIScreen.mainScreen.bounds 或 Auto Layout。

引導頁應該只顯示一次。 動畫結束後把「已看過引導」寫入 NSUserDefaults,下次啟動直接跳過,否則每次開 App 都要滑一遍會很惱人。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved