欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖效果

 更新時間:2018年07月22日 09:09:15   作者:Leemin_ios  
這篇文章主要介紹了iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS使用UIScrollView實現(xiàn)無限循環(huán)輪播圖的具體代碼,供大家參考,具體內(nèi)容如下

代碼:

//
// ViewController.m
// 無限輪播
//
// Created by limin on 17/8/23.
// Copyright © 2017年 none. All rights reserved.
//
 
#import "ViewController.h"
 
@interface ViewController ()<UIScrollViewDelegate>
/* 定時器 */
@property(nonatomic,strong)NSTimer *rotateTimer;
/* */
@property(nonatomic,strong)UIPageControl *myPageControl;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
 [super viewDidLoad];
 //初始化scroolview大小為屏幕大小
 UIScrollView *rotateScrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
 //設(shè)置滾動范圍
 rotateScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3, CGRectGetHeight(self.view.frame));
 //設(shè)置分頁效果
 rotateScrollView.pagingEnabled = YES;
 //水平滾動條隱藏
 rotateScrollView.showsHorizontalScrollIndicator = NO;
 //添加三個子視圖,uilabel類型
 for (int i=0; i<3; i++) {
 UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
 subLabel.tag = 1000+i;
 subLabel.text = [NSString stringWithFormat:@"我是第%d個視圖",i];
 [subLabel setFont:[UIFont systemFontOfSize:80]];
 subLabel.adjustsFontSizeToFitWidth = YES;
 [subLabel setBackgroundColor:[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]];
 [rotateScrollView addSubview:subLabel];
 
 }
 UILabel *tempLabel = [rotateScrollView viewWithTag:1000];
 //為滾動視圖的右邊添加一個視圖,使得它和第一個視圖一模一樣。
 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*3, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
 label.backgroundColor = tempLabel.backgroundColor;
 label.text = tempLabel.text;
 label.font = tempLabel.font;
 label.adjustsFontSizeToFitWidth = YES;
 [rotateScrollView addSubview:label];
 [self.view addSubview:rotateScrollView];
 rotateScrollView.tag = 1000;
 self.myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-50, CGRectGetWidth(self.view.frame), 50)];
 self.myPageControl.numberOfPages = 3;
 self.myPageControl.currentPage = 0;
 [self.view addSubview:self.myPageControl];
 
 //啟動定時器
 self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeView) userInfo:nil repeats:YES];
 //為滾動視圖指定代理
 rotateScrollView.delegate = self;
}
 
#pragma mark -- 滾動視圖的代理方法
//開始拖拽的代理方法,在此方法中暫停定時器。
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
 NSLog(@"正在拖拽視圖,所以需要將自動播放暫停掉");
 //setFireDate:設(shè)置定時器在什么時間啟動
 //[NSDate distantFuture]:將來的某一時刻
 [self.rotateTimer setFireDate:[NSDate distantFuture]];
}
 
//視圖靜止時(沒有人在拖拽),開啟定時器,讓自動輪播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
 //視圖靜止之后,過1.5秒在開啟定時器
 //[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]] 返回值為從現(xiàn)在時刻開始 再過1.5秒的時刻。
 NSLog(@"開啟定時器");
 [self.rotateTimer setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]];
}
 
 
//定時器的回調(diào)方法 切換界面
- (void)changeView{
 //得到scrollView
 UIScrollView *scrollView = [self.view viewWithTag:1000];
 //通過改變contentOffset來切換滾動視圖的子界面
 float offset_X = scrollView.contentOffset.x;
 //每次切換一個屏幕
 offset_X += CGRectGetWidth(self.view.frame);
 
 //說明要從最右邊的多余視圖開始滾動了,最右邊的多余視圖實際上就是第一個視圖。所以偏移量需要更改為第一個視圖的偏移量。
 if (offset_X > CGRectGetWidth(self.view.frame)*3) {
 scrollView.contentOffset = CGPointMake(0, 0);
 
 }
 //說明正在顯示的就是最右邊的多余視圖,最右邊的多余視圖實際上就是第一個視圖。所以pageControl的小白點需要在第一個視圖的位置。
 if (offset_X == CGRectGetWidth(self.view.frame)*3) {
 self.myPageControl.currentPage = 0;
 }else{
 self.myPageControl.currentPage = offset_X/CGRectGetWidth(self.view.frame);
 }
 
 //得到最終的偏移量
 CGPoint resultPoint = CGPointMake(offset_X, 0);
 //切換視圖時帶動畫效果
 //最右邊的多余視圖實際上就是第一個視圖,現(xiàn)在是要從第一個視圖向第二個視圖偏移,所以偏移量為一個屏幕寬度
 if (offset_X >CGRectGetWidth(self.view.frame)*3) {
 self.myPageControl.currentPage = 1;
 [scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.view.frame), 0) animated:YES];
 }else{
 [scrollView setContentOffset:resultPoint animated:YES];
 }
 
}
 
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
 
 
@end

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論