温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS基于UIScrollView如何实现滑动引导页

发布时间:2021-07-07 12:53:25 来源:亿速云 阅读:154 作者:小新 栏目:移动开发

这篇文章将为大家详细讲解有关iOS基于UIScrollView如何实现滑动引导页,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

效果图:

iOS基于UIScrollView如何实现滑动引导页

WelcomeViewController.h

#import <UIKit/UIKit.h> 
 
@interface WelcomeViewController : UIViewController 
 
@end

WelcomeViewController.m 

#import "WelcomeViewController.h" 
#define IMAGECOUNT 3 
 
@interface WelcomeViewController () <UIScrollViewDelegate> 
@property (nonatomic, strong)UIPageControl *pageControl; 
 
@end 
 
@implementation WelcomeViewController 
 
- (void)viewDidLoad { 
  [super viewDidLoad]; 
  //创建ScrollView 
  UIScrollView *sv = [[UIScrollView alloc] init]; 
  sv.frame = self.view.bounds; 
  //设置边缘不弹跳 
  sv.bounces = NO; 
  //整页滚动 
  sv.pagingEnabled = YES; 
  sv.showsHorizontalScrollIndicator = NO; 
   
  //加入多个子视图(ImageView) 
  for(NSInteger i=0; i<IMAGECOUNT; i++){ 
    NSString *imgName = [NSString stringWithFormat:@"%ld", i+1]; 
    UIImage *image = [UIImage imageNamed:imgName]; 
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; 
    CGRect frame = CGRectZero; 
    frame.origin.x = i * sv.frame.size.width; 
    frame.size = sv.frame.size; 
    imageView.frame = frame; 
    [sv addSubview:imageView]; 
     
    if(i==IMAGECOUNT-1){ 
      //开启图片的用户点击功能 
      imageView.userInteractionEnabled = YES; 
      //加个按钮 
      UIButton *button = [[UIButton alloc]init]; 
       
      button.frame = CGRectMake((imageView.frame.size.width-150)/2, imageView.frame.size.height*0.8, 150, 40); 
      button.backgroundColor = [UIColor orangeColor]; 
      [button setTitle:@"立即体验" forState:UIControlStateNormal]; 
      button.titleLabel.font = [UIFont boldSystemFontOfSize:16]; 
      [imageView addSubview:button]; 
      [button addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];    } 
  } 
   
  sv.contentSize = CGSizeMake(IMAGECOUNT * sv.frame.size.width, sv.frame.size.height); 
   
  [self.view addSubview:sv]; 
   
  //加入页面指示控件PageControl 
  UIPageControl *pageControl = [[UIPageControl alloc]init]; 
  self.pageControl = pageControl; 
  //设置frame 
  pageControl.frame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 20); 
  //分页面的数量 
  pageControl.numberOfPages = IMAGECOUNT; 
  //设置小圆点渲染颜色 
  pageControl.pageIndicatorTintColor = [UIColor whiteColor]; 
  //设置当前选中小圆点的渲染颜色 
  pageControl.currentPageIndicatorTintColor = [UIColor redColor]; 
  //关闭用户点击交互 
  pageControl.userInteractionEnabled = NO; 
   
  [self.view addSubview:pageControl]; 
   
  sv.delegate = self; 
   
   
} 
- (void)enter 
{ 
  NSLog(@"进入应用"); 
} 
 
//UIScrollViewDelegate方法 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
  CGPoint offset = scrollView.contentOffset; 
  if(offset.x<=0){ 
    offset.x = 0; 
    scrollView.contentOffset = offset; 
  } 
  NSUInteger index = round(offset.x / scrollView.frame.size.width); 
  self.pageControl.currentPage = index; 
} 
 
- (void)didReceiveMemoryWarning { 
  [super didReceiveMemoryWarning]; 
  // Dispose of any resources that can be recreated. 
} 
 
 
@end

关于“iOS基于UIScrollView如何实现滑动引导页”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI