引导页GuideViewController的写法

芮瑾瑜
2023-12-01

今天写的是一个常用的ScrollView的滑动页,效果类似于PageControl

GuideViewController.h

@interface GuideViewController : UIViewController<UIScrollViewDelegate>

@property (retain, nonatomic) IBOutlet UIScrollView *guideScrollview;

@property (retain, nonatomic) IBOutlet UIPageControl *guidePageControl;


GuideViewController.m


#import "GuideViewController.h"


@interface GuideViewController ()


@end


@implementation GuideViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.frame = CGRectMake(0, 20, 320, IS_IPHONE5?(460+88):460);

    self.guideScrollview.contentSize = CGSizeMake(320*4, IS_IPHONE5?(460+88):460);

    UIImageView *help1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"user%@_1",IS_IPHONE5?@"5":@"4"]]];

    help1.frame = CGRectMake(0, 0, 320, IS_IPHONE5?(460+88):460);

    UIImageView *help2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"user%@_2",IS_IPHONE5?@"5":@"4"]]];

    help2.frame = CGRectMake(320, 0, 320, IS_IPHONE5?(460+88):460);

    UIImageView *help3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"user%@_3",IS_IPHONE5?@"5":@"4"]]];

    help3.frame = CGRectMake(640, 0, 320, IS_IPHONE5?(460+88):460);

    UIImageView *help4 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"user%@_4",IS_IPHONE5?@"5":@"4"]]];

    help4.frame = CGRectMake(960, 0, 320, IS_IPHONE5?(460+88):460);

    

    [self.guideScrollview addSubview:help1];

    [self.guideScrollview addSubview:help2];

    [self.guideScrollview addSubview:help3];

    [self.guideScrollview addSubview:help4];

    

    [help1 release];

    [help2 release];

    [help3 release];

    [help4 release];

    

    self.guidePageControl.numberOfPages=self.guideScrollview.contentSize.width/320;

}


// 已经结束拖拽,手指刚离开view的那一刻

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    if (scrollView.contentOffset.x > 1020) {

        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

        // app版本

        NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

        [[NSUserDefaults standardUserDefaults]setObject:app_Version forKey:@"versions"];

        scrollView.contentSize = CGSizeMake(320*5, IS_IPHONE5?(460+88):460);

        [UIView animateWithDuration:1.0 animations:^{

            self.view.center = CGPointMake(self.view.center.x-320,self.view.center.y);

            self.view.alpha = 0;

        } completion:^(BOOL finished) {

            [self.view removeFromSuperview];

            [self release];

        }];

    }

}


//停止滚动的时候

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    self.guidePageControl.currentPage = self.guideScrollview.contentOffset.x/320;

    [self.guidePageControl updateCurrentPageDisplay];

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)dealloc {

    [_guideScrollview release];

    [_guidePageControl release];

    [super dealloc];

}

- (void)viewDidUnload {

    [self setGuideScrollview:nil];

    [self setGuidePageControl:nil];

    [super viewDidUnload];

}

@end



 类似资料: