UIScrollView和UIPageController

这里写图片描述

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];

    //创建导航栏控制器
    UINavigationController* nav = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];

    //设置应用程序的根控制器
    self.window.rootViewController = nav;
    return YES;
}
#import "ViewController.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define Heigth [UIScreen mainScreen].bounds.size.heigth

@interface ViewController ()<UIScrollViewDelegate>

@property (retain)UIScrollView* scrollView;
@property (retain)UIPageControl* pageControl;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];

    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake((Width-320)/2, 65, 320, 480)];
    self.scrollView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.scrollView];

    //要设置代理⚠️
    self.scrollView.delegate=self;

    for (NSInteger i = 0; i < 4; i++) {
        UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 480)];
        [self.scrollView addSubview:imageView];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"美女0%ld.jpg",i+1]];
        //防止内容视图下滑
        self.automaticallyAdjustsScrollViewInsets = NO;
        //设置滚动范围
        self.scrollView.contentSize = CGSizeMake(320*4, 480);
        //取消反弹效果
        self.scrollView.bounces = NO;
        //隐藏水平方向的滑块
        self.scrollView.showsHorizontalScrollIndicator = NO;
        //设置分页显示
        self.scrollView.pagingEnabled = YES;

        //创建UIPaneControl对象
        self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((Width-100)/2, 450+64, 100, 30)];
        self.pageControl.backgroundColor = [UIColor clearColor];
        //设置总页数
        self.pageControl.numberOfPages = 4;
        //设置当前页
        self.pageControl.currentPage = 0;

        //让self.pageControl绑定事件
        [self.pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:self.pageControl];
    }
    //左边按钮
    UIButton* leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 60, 30);
    leftBtn.backgroundColor = [UIColor grayColor];
    [leftBtn setTitle:@"上一页" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    //右边按钮
    UIButton* rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 60, 30);
    rightBtn.backgroundColor = [UIColor grayColor];
    [rightBtn setTitle:@"下一页" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];

}

-(void)leftAction:(UIBarButtonItem*)sender
{
    NSLog(@"上一页被点!!!!");
    NSInteger page = self.pageControl.currentPage;
    if (page > 0) {
        page--;
        [UIView animateWithDuration:0.5 animations:^{
            self.pageControl.currentPage = page;
            self.scrollView.contentOffset = CGPointMake(320*page, 0);
        }];
    }
}

-(void)rightAction:(UIBarButtonItem*)sender
{
    NSLog(@"下一页被点!!!!");
    NSInteger page = self.pageControl.currentPage;
    if (page < 3) {
        page++;
        [UIView animateWithDuration:0.5 animations:^{
            self.pageControl.currentPage = page;
            self.scrollView.contentOffset = CGPointMake(320*page, 0);
        }];
    }
}

-(void)pageAction:(UIPageControl*)page
{
    NSLog(@"%ld",(long)page.currentPage);
    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = CGPointMake(page.currentPage*320, 0);
    }];
}

#pragma mark-UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"hehe");
    //获取偏移量
    CGPoint point = scrollView.contentOffset;
    NSLog(@"%@",NSStringFromCGPoint(point));
    //计算出当前展示的是第几张照片
    NSInteger page = point.x/300;
    self.pageControl.currentPage = page;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
定时滚动和循环滚动,可点击图片和PageController #import "ASIFormDataRequest.h" #import "GWPublicClass.h" @interface ViewController ()<UIScrollViewDelegate> @end @implementation ViewController { UIScrollView * headScrollView; UIPageControl * pageControl; NSArray * colorArray; NSTimer * myTimer; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } #pragma UIScrollView delegate -(void)scrollToNextPage:(id)sender { int pageNum = pageControl.currentPage; CGSize viewSize = headScrollView.frame.size; CGRect rect = CGRectMake((pageNum+2)*viewSize.width, 0, viewSize.width, viewSize.height); [headScrollView scrollRectToVisible:rect animated:NO]; pageNum++; if (pageNum == colorArray.count) { CGRect newRect=CGRectMake(viewSize.width, 0, viewSize.width, viewSize.height); [headScrollView scrollRectToVisible:newRect animated:NO]; } } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat pageWidth = headScrollView.frame.size.width; int currentPage = floor((headScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1; if (currentPage == 0) { pageControl.currentPage = colorArray.count-1; }else if(currentPage == colorArray.count+1){ pageControl.currentPage=0; } pageControl.currentPage = currentPage-1; } -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [myTimer invalidate]; } -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES]; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGFloat pageWidth = headScrollView.frame.size.width; CGFloat pageHeigth = headScrollView.frame.size.height; int currentPage=floor((headScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1; NSLog(@"the current offset==%f",headScrollView.contentOffset.x); NSLog(@"the current page==%d",currentPage); if (currentPage == 0) { [headScrollView scrollRectToVisible:CGRectMake(pageWidth*colorArray.count, 0, pageWidth, pageHeigth) animated:NO]; pageControl.currentPage = colorArray.count-1; NSLog(@"pageControl currentPage==%d",pageControl.currentPage); NSLog(@"the last image"); return; }else if(currentPage == [colorArray count]+1){ [headScrollView scrollRectToVisible:CGRectMake(pageWidth, 0, pageWidth, pageHeigth) animated:NO]; pageControl.currentPage=0; NSLog(@"pageControl currentPage==%d",pageControl.currentPage); NSLog(@"the first image"); return; } pageControl.currentPage=currentPage-1; NSLog(@"pageControl currentPage==%d",pageControl.currentPage); } - (void)pageTurn:(UIPageControl *)sender { int pageNum = pageControl.currentPage; CGSize viewSize = headScrollView.frame.size; [headScrollView setContentOffset:CGPointMake((pageNum+1)*viewSize.width, 0)]; NSLog(@"myscrollView.contentOffSet.x==%f",headScrollView.contentOffset.x); NSLog(@"pageControl currentPage==%d",pageControl.currentPage); [myTimer invalidate]; } - (void)handleTapGesture:(UITapGestureRecognizer*)gesture { NSLog(@"UITapGesture被调用了%d",gesture.view.tag); // ... } - (void)viewDidLoad { [super viewDidLoad]; colorArray = @[[UIColor redColor],[UIColor greenColor],[UIColor blueColor]]; headScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 320, 200)]; headScrollView.backgroundColor = [UIColor blackColor]; [self.view addSubview:headScrollView]; CGFloat Width= 320; CGFloat Height= 200; UIImageView * firstImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)]; firstImageView.userInteractionEnabled = YES; firstImageView.tag = 50+colorArray.count-1; firstImageView.backgroundColor = colorArray[colorArray.count-1]; [headScrollView addSubview:firstImageView]; UITapGestureRecognizer * gestd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [firstImageView addGestureRecognizer:gestd]; for (int i=0; i<colorArray.count; i++) { UIImageView * subImageView=[[UIImageView alloc] initWithFrame:CGRectMake(Width*(i+1), 0, Width, Height)]; subImageView.backgroundColor = colorArray[i]; subImageView.userInteractionEnabled = YES; subImageView.tag = 50+i; subImageView.frame=CGRectMake(Width*(i+1), 0, Width, Height); [headScrollView addSubview: subImageView]; UITapGestureRecognizer * gestd = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [subImageView addGestureRecognizer:gestd]; } UIImageView * lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Width*(colorArray.count+1), 0, Width, Height)]; lastImageView.userInteractionEnabled = YES; lastImageView.backgroundColor = colorArray[0]; lastImageView.tag = 50; [headScrollView addSubview:lastImageView]; headScrollView.contentSize = CGSizeMake(Width*(colorArray.count+2), Height); headScrollView.pagingEnabled = YES; headScrollView.delegate = self; [headScrollView scrollRectToVisible:CGRectMake(Width, 0, Width, Height) animated:YES]; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 290, 120, 20)]; pageControl.numberOfPages = colorArray.count; pageControl.backgroundColor = [UIColor greenColor]; pageControl.enabled = YES; pageControl.currentPage = 0; [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:pageControl]; myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES]; } - (void)enterDetail { NSLog(@"tap is "); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值