UIScrollView 加载多个view view还可以交换顺序(1)
#import "ViewController.h"
#define kWidthOfScreen self.view.frame.size.width
#define kHeightOfScreen self.view.frame.size.height
@interface ViewController ()
{
UIScrollView *scrollview;
UIView *viewleft;
UIView *viewcenter;
UIView *viewright;
UILabel *labelleft;
UILabel *labelcenter;
UILabel *labelright;
UIView *view8;
UIView *view1;
UIView *view2;
UIView *view3;
UIView *view4;
UIView *view5;
UIView *view6;
UIView *view7;
NSInteger _viewCount;//view的总数
NSInteger viewindex;//view存入的索引
NSInteger _currentviewIndex;//当前图片索引
NSMutableArray *arry1;
NSMutableArray *arry2;
UIPageControl *_pageControl;
UISwipeGestureRecognizer *leftSwipeGestureRecognizer;//上的手势
UISwipeGestureRecognizer *rightSwipeGestureRecognizer;//下的手势
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initscrollview];
[self initobjctview];
[self initgestures];
[self initPageControl];
[self initaddviewdata];
[self setDefaultview]; //加载默认view
}
#pragma mark - scrollview的创建
-(void)initscrollview{
scrollview = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
scrollview.backgroundColor = [UIColor yellowColor];
scrollview.delegate = self;
scrollview.pagingEnabled = YES;
scrollview.showsHorizontalScrollIndicator = NO;
scrollview.contentSize=CGSizeMake(3*kWidthOfScreen, 0) ;
[scrollview setContentOffset:CGPointMake(kWidthOfScreen, 0) animated:NO];//设置当前显示的位置为中间图片
[self.view addSubview:scrollview];
}
#pragma mark -创建父类view
-(void)initobjctview{
//图片视图;左边
viewleft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
viewleft.backgroundColor = [UIColor redColor];
viewleft.contentMode = UIViewContentModeScaleAspectFit;
[scrollview addSubview:viewleft];
//图片视图;中间
viewcenter = [[UIView alloc] initWithFrame:CGRectMake(kWidthOfScreen, 0, kWidthOfScreen, kHeightOfScreen)];
viewcenter.contentMode = UIViewContentModeScaleAspectFit;
viewcenter.backgroundColor = [UIColor yellowColor];
[scrollview addSubview:viewcenter];
//图片视图;右边
viewright = [[UIView alloc] initWithFrame:CGRectMake(kWidthOfScreen * 2, 0, kWidthOfScreen, kHeightOfScreen)];
viewright.contentMode = UIViewContentModeScaleAspectFit;
viewright.backgroundColor = [UIColor blueColor];
[scrollview addSubview:viewright];
labelleft = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelleft.text = @"小子这是第一个你知道不";
[viewleft addSubview:labelleft];
labelcenter = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelcenter.text = @"这就是中间";
[viewcenter addSubview:labelcenter];
labelright = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelright.text = @"哎哟我在最后了";
[viewright addSubview:labelright];
}
#pragma mark - 创建手势
-(void)initgestures{
leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
}
#pragma mark- 分页控件创建
-(void)initPageControl{
_pageControl=[[UIPageControl alloc]init];
CGSize size= [_pageControl sizeForNumberOfPages:_viewCount];
_pageControl.bounds=CGRectMake(0, 0, size.width, size.height);
_pageControl.center=CGPointMake(kWidthOfScreen/2, kHeightOfScreen-100);
_pageControl.pageIndicatorTintColor=[UIColor colorWithRed:193/255.0 green:219/255.0 blue:249/255.0 alpha:1];
_pageControl.currentPageIndicatorTintColor=[UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
_pageControl.numberOfPages=_viewCount; //设置总页数
[self.view addSubview:_pageControl];
}
#pragma mark - 视图集合
-(void)initaddviewdata{
view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view1.contentMode = UIViewContentModeScaleAspectFit;
view1.backgroundColor = [UIColor redColor];
view1.tag = 1;
UILabel *labelview1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview1.text = @"第一个页面";
[view1 addSubview:labelview1];
view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view2.contentMode = UIViewContentModeScaleAspectFit;
view2.backgroundColor = [UIColor yellowColor];
view2.tag = 2;
UILabel *labelview2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview2.text = @"第二个页面";
[view2 addSubview:labelview2];
view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view3.contentMode = UIViewContentModeScaleAspectFit;
view3.backgroundColor = [UIColor brownColor];
view2.tag = 3;
UILabel *labelview3 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview3.text = @"第三个页面";
[view3 addSubview:labelview3];
view4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view4.contentMode = UIViewContentModeScaleAspectFit;
view4.backgroundColor = [UIColor blueColor];
view2.tag = 4;
UILabel *labelview4 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview4.text = @"第四个页面";
[view4 addSubview:labelview4];
view5 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view5.contentMode = UIViewContentModeScaleAspectFit;
view5.backgroundColor = [UIColor greenColor];
view2.tag = 5;
UILabel *labelview5 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview5.text = @"第五个页面";
[view5 addSubview:labelview5];
view6 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view6.contentMode = UIViewContentModeScaleAspectFit;
view6.backgroundColor = [UIColor lightGrayColor];
view2.tag = 6;
UILabel *labelview6 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview6.text = @"第六个页面";
[view6 addSubview:labelview6];
view7 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view7.contentMode = UIViewContentModeScaleAspectFit;
view7.backgroundColor = [UIColor whiteColor];
view2.tag = 7;
UILabel *labelview7 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview7.text = @"第七个页面";
[view7 addSubview:labelview7];
view8 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidthOfScreen, kHeightOfScreen)];
view8.contentMode = UIViewContentModeScaleAspectFit;
view8.backgroundColor = [UIColor orangeColor];
view2.tag = 8;
UILabel *labelview8 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 25)];
labelview8.text = @"第八个页面";
[view8 addSubview:labelview8];
//取出数据
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
viewindex = [standardUserDefaults integerForKey:@"currentImageIndex"];
NSInteger data0 = [standardUserDefaults integerForKey:@"viewnumber0"];
NSInteger data1 = [standardUserDefaults integerForKey:@"viewnumber1"];
NSInteger data2 = [standardUserDefaults integerForKey:@"viewnumber2"];
NSInteger data3 = [standardUserDefaults integerForKey:@"viewnumber3"];
NSInteger data4 = [standardUserDefaults integerForKey:@"viewnumber4"];
NSInteger data5 = [standardUserDefaults integerForKey:@"viewnumber5"];
NSInteger data6 = [standardUserDefaults integerForKey:@"viewnumber6"];
NSInteger data7 = [standardUserDefaults integerForKey:@"viewnumber7"];
// NSLog(@"返回数据%d",data0);
// NSLog(@"返回数据%d",data1);
// NSLog(@"返回数据%d",data2);
// NSLog(@"返回数据%d",data3);
// NSLog(@"返回数据%d",data4);
// NSLog(@"返回数据%d",data5);
// NSLog(@"返回数据%d",data6);
// NSLog(@"返回数据%d",data7);
if (data0) {
NSArray *arry3;
NSNumber *number0 =[NSNumber numberWithInteger:data0];
NSNumber *number1 =[NSNumber numberWithInteger:data1];
NSNumber *number2 =[NSNumber numberWithInteger:data2];
NSNumber *number3 =[NSNumber numberWithInteger:data3];
NSNumber *number4 =[NSNumber numberWithInteger:data4];
NSNumber *number5 =[NSNumber numberWithInteger:data5];
NSNumber *number6 =[NSNumber numberWithInteger:data6];
NSNumber *number7 =[NSNumber numberWithInteger:data7];
arry1 = [[NSMutableArray alloc]initWithObjects:number0,number1,number2,number3,number4,number5,number6,number7, nil];
arry3 = [[NSMutableArray alloc]initWithObjects:view1,view2,view3,view4,view5,view6,view7,view8, nil];
arry2 = [[NSMutableArray alloc]initWithObjects:arry3[data0],arry3[data1],arry3[data2],arry3[data3],arry3[data4],arry3[data5],arry3[data6],arry3[data7], nil];
}else{
NSNumber *number0 =[NSNumber numberWithInt:0];
NSNumber *number1 =[NSNumber numberWithInt:1];
NSNumber *number2 =[NSNumber numberWithInt:2];
NSNumber *number3 =[NSNumber numberWithInt:3];
NSNumber *number4 =[NSNumber numberWithInt:4];
NSNumber *number5 =[NSNumber numberWithInt:5];
NSNumber *number6 =[NSNumber numberWithInt:6];
NSNumber *number7 =[NSNumber numberWithInt:7];
arry1 = [[NSMutableArray alloc]initWithObjects:number0,number1,number2,number3,number4,number5,number6,number7, nil];
arry2 = [[NSMutableArray alloc]initWithObjects:view1,view2,view3,view4,view5,view6,view7,view8, nil];
}
_viewCount =arry2.count;
NSLog(@"打印出的索引为%ld",viewindex);
NSLog(@"打印出view的个数%ld",_viewCount);
}
#pragma mark - 手势方法
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{ //向上
if (sender.direction == UISwipeGestureRecognizerDirectionUp)
{
if (_currentviewIndex==0) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示"message:@"这已经是第一个了"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"Okay");
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
}else{
[arry1 exchangeObjectAtIndex:_currentviewIndex withObjectAtIndex:_currentviewIndex-1];//数组数据的交换
[arry2 exchangeObjectAtIndex:_currentviewIndex withObjectAtIndex:_currentviewIndex-1];
[arry2[_currentviewIndex] addGestureRecognizer:leftSwipeGestureRecognizer];
[arry2[_currentviewIndex] addGestureRecognizer:rightSwipeGestureRecognizer];
NSLog(@"上上上");
[self reloadImage];
}
}
//向下
if (sender.direction == UISwipeGestureRecognizerDirectionDown) {
if (_currentviewIndex==7) {
//弹出提示
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示"message:@"这已经是最后一个了"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
}else{
[arry2 exchangeObjectAtIndex:_currentviewIndex withObjectAtIndex:_currentviewIndex+1];//数组里面数据的交换
[arry1 exchangeObjectAtIndex:_currentviewIndex withObjectAtIndex:_currentviewIndex+1];
[arry2[_currentviewIndex] addGestureRecognizer:leftSwipeGestureRecognizer];
[arry2[_currentviewIndex] addGestureRecognizer:rightSwipeGestureRecognizer];
NSLog(@"下下下下");
[self reloadImage];
}
}
}
#pragma mark 设置默认视图显示
-(void)setDefaultview{
if (viewindex) {
if (viewindex==0)
{
_currentviewIndex =viewindex;
[viewleft addSubview:arry2[7]];
[viewcenter addSubview:arry2[0]];
[viewright addSubview:arry2[1]];
}else
if( viewindex ==7){
_currentviewIndex =viewindex;
[viewleft addSubview:arry2[6]];
[viewcenter addSubview:arry2[7]];
[viewright addSubview:arry2[0]];
}else {
_currentviewIndex =viewindex;
[viewleft addSubview:arry2[_currentviewIndex-1]];
[viewcenter addSubview:arry2[_currentviewIndex]];
[viewright addSubview:arry2[_currentviewIndex+1]];
}
}else{
[viewleft addSubview:arry2[7]];
[viewcenter addSubview:arry2[0]];
[viewright addSubview:arry2[1]];
_currentviewIndex=0;
_pageControl.currentPage=_currentviewIndex; //设置当前页
}
}
#pragma mark scrollview代理 滚动结束执行
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//重新加载页面
[self reloadImage];
//存入数据
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:arry1[0] forKey:@"viewnumber0"];
[standardUserDefaults setObject:arry1[1] forKey:@"viewnumber1"];
[standardUserDefaults setObject:arry1[2] forKey:@"viewnumber2"];
[standardUserDefaults setObject:arry1[3] forKey:@"viewnumber3"];
[standardUserDefaults setObject:arry1[4] forKey:@"viewnumber4"];
[standardUserDefaults setObject:arry1[5] forKey:@"viewnumber5"];
[standardUserDefaults setObject:arry1[6] forKey:@"viewnumber6"];
[standardUserDefaults setObject:arry1[7] forKey:@"viewnumber7"];
//存入固定索引
[standardUserDefaults setInteger:_currentviewIndex forKey:@"currentImageIndex"]; //存入当前显示页面脚本号
[standardUserDefaults synchronize];
[scrollview setContentOffset:CGPointMake(kWidthOfScreen, 0) animated:NO]; //移动到中间
_pageControl.currentPage=_currentviewIndex; //设置分页
NSLog(@"%ld",(long)_currentviewIndex);
//设置当前页面的手势
[arry2[_currentviewIndex] addGestureRecognizer:leftSwipeGestureRecognizer];
[arry2[_currentviewIndex] addGestureRecognizer:rightSwipeGestureRecognizer];
}
#pragma mark 重新加页面
-(void)reloadImage{
int leftviewIndex,rightviewIndex;
CGPoint offset=[scrollview contentOffset];
if (offset.x>kWidthOfScreen) { //向右滑动
_currentviewIndex=(int)(_currentviewIndex+1)%_viewCount;
}else if(offset.x _currentviewIndex=(int)(_currentviewIndex+_viewCount-1)%_viewCount;
}
[viewcenter addSubview:arry2[_currentviewIndex]];
//重新设置左右view
leftviewIndex =
(int)((_currentviewIndex+_viewCount-1)%_viewCount);
rightviewIndex = (int)((_currentviewIndex+1)%_viewCount);
[viewleft addSubview:arry2[leftviewIndex]];
[viewright addSubview:arry2[rightviewIndex]];
}
#pragma mark -添加信息描述控件
-(void)addLabel{
// _label=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH,30)];
// _label.textAlignment=NSTextAlignmentCenter;
// _label.textColor=[UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];
// [self.view addSubview:_label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
UIScrollView 加载多个view view还可以交换顺序(1)的更多相关文章
- 浏览器环境下JavaScript脚本加载与执行探析之代码执行顺序
本文主要基于向HTML页面引入JavaScript的几种方式,分析HTML中JavaScript脚本的执行顺序问题 1. 关于JavaScript脚本执行的阻塞性 JavaScript在浏览器中被解析 ...
- UIScrollView 加载多个view view还可以交换顺序(2)
在上的代码中 UIview都在一个页面 要是每一个view对应一个页面 操作每一个view代码都在viewcontroller里面就会很乱,那么我们就将view重新做一下 创建想要的页面继承与view ...
- Swift控制器加载xib Swift Controller'view load from xib
override func loadView() { NSBundle.mainBundle().loadNibNamed("ViewController", owner: sel ...
- 【Android - 自定义View】之自定义可下拉刷新或上拉加载的ListView
首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 RefreshableListView ,继承自ListView类: (2)在这个自定义View中,用户可以设置是否支持下拉刷新 ...
- IT蓝豹--RecyclerView加载不同view实现效果
本项目由开发者:黄洞洞精心为初学者编辑RecyclerView的使用方法. RecyclerView加载不同view实现效果,支持加载多个view,并且支持用volley获取数据, 项目主要介绍: 初 ...
- 控制器View的加载过程
1.控制器内部的view是延迟加载 1> 用到时再加载2> 加载完毕后会调用控制器的viewDidLoad方法 2.创建控制器的方式 1> 直接通过代码创建OneViewContro ...
- Android学习笔记_31_通过后台代码生成View对象以及动态加载XML布局文件到LinearLayout
一.布局文件part.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- view的加载
这是一个listpopwindow的布局,如果listview在relativeLayout之后写的那么listview就会把relativeLayout给覆盖掉,这证明布局的加载是按照布局文件写的先 ...
- vue2.0中,由于页面完成之后dom还未加载完成如何进行操作
再vue中,当页面加载完成以后,dom还没有加载,是无法获取进行操作的,但是在vue2.0中提供了一个方法:this.$nextTick,在这个回调函数里面写dom操作即可: 如下代码: create ...
随机推荐
- 《css揭秘》
<css揭秘> 第一章:引言 引言 案例们 第二章:背景与边框 背景和边框 半透明边框(rgba/hsla.background-clip) 多重边框(box-shadow) 灵活的背景定 ...
- [SQL基础教程] 3-4 对查询结果进行排序/ORDER BY
[SQL基础教程] 3-4 对查询结果进行排序/ORDER BY ORDER BY SELECT <列名1>,<列名2>,<列名2>... FROM ORDER B ...
- Topself 方便调试的Window服务框架
Installing Topshelf nuget Install-Package Topshelf public class TownCrier { readonly Timer _timer; p ...
- 实战荟萃-UI篇
一. 前言 平时在处理问题的时候,经常会遇到一些奇奇怪怪的问题,今天在这里将其记录下来.这里将会列举几个常用的UI问题进行讲解 二. 导航栏 iOS导航栏绝对是个巨坑.和很多朋友聊天都是自己实现了一套 ...
- 关于PHP的一小段代码求解如下求解"%2\$s"
<?php$format = "The %2\$s contains %1\$d monkeys";printf($format, 8, "北京");?& ...
- 12C RMAN 备份参考v1
windows bat 1,C:\dba\utility\rman\rman.bat del C:\dba\utility\rman\full_db_* /qset TNSNAME=ceipuatrm ...
- Cron表达式的详细用法
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN- ...
- cuckoo数据库变更
1.cuckoo版本升级 cuckoo默认的数据库为sqlite,默认连接方式为sqlite:///os.path.join(REPORT_ROOT, "db", "cu ...
- C#动态编程
class Program { static void Main(string[] args) { Test(); } public static void Test() { //声明代码的部分 Co ...
- MQ队列堆积太长,消费不过来怎么办(转)
转自:http://windwrite.com/archives/603 我们现有的业务就面临此问题,消息生产太快,消费不过来,导致队列堆积很长,把服务器内存耗尽,这时RabbitMQ的处理能力很低下 ...