左右滑动加点击跳转
类似于答题功能,可以实现左右滑动到下一页面,或者能过点击跳转到下一题
直接贴代码
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"左右滑动+Btn滑动";
[selfcreatSubview];
}
//创建子view
- (void)creatSubview
{
_mArray = [NSMutableArraynew];
NSInteger heigth = self.view.frame.size.height;
NSInteger width = self.view.frame.size.width;
_scrollview = [[UIScrollViewalloc] initWithFrame:CGRectMake(0, 0, width, heigth)];
_scrollview.backgroundColor = [UIColorwhiteColor];
_scrollview.pagingEnabled = YES;
_scrollview.scrollEnabled=YES;
_scrollview.showsHorizontalScrollIndicator=NO;
_scrollview.showsVerticalScrollIndicator=NO;
_scrollview.delegate=self;
[self.viewaddSubview:_scrollview];
_array=[NSArrayarrayWithObjects:[UIColororangeColor],[UIColorpurpleColor], [UIColorwhiteColor],[UIColorredColor],[UIColorgreenColor],[UIColorgrayColor],nil];
UIView * view =[[UIViewalloc] initWithFrame:CGRectMake(_pageNumber*_scrollview.frame.size.width,0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
[_scrollviewaddSubview:view];
_scrollview.contentSize=CGSizeMake(width * 7, 0);
_btn = [UIButtonbuttonWithType:(UIButtonTypeCustom)];
_btn.frame = CGRectMake(width / 3, heigth / 3, width / 3, 30);
_btn.backgroundColor = [UIColorblueColor];
[_btnsetTitle:@"0"forState:(UIControlStateNormal)];
[_btnaddTarget:selfaction:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.viewaddSubview:_btn];
}
//button事件
- (void)btnAction:(UIButton * )btn
{
if (_pageNumber != _array.count - 1) {
_pageNumber++;
}else{
NSLog(@"The last one");
}
//动态创建view
[selfcreatView:_pageNumber];
// NSLog(@"click");
[btn setTitle:[NSStringstringWithFormat:@"%ld",(long)_pageNumber] forState:(UIControlStateNormal)];
CGFloat x = _pageNumber * self.scrollview.frame.size.width;
_scrollview.contentOffset = CGPointMake(x,0);
}
#pragma mark - scrollView的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//计算相应地页(滚动视图可以滚动的总宽度/单个滚动视图的宽度=滚动视图的页数)
NSInteger currentPage = (int)(scrollView.contentOffset.x) / (int)(self.view.frame.size.width);
_pageNumber = currentPage;//将上述的滚动视图页数重新赋给当前视图页数,进行分页
[selfcreatView:_pageNumber];
[_btnsetTitle:[NSStringstringWithFormat:@"%ld",_pageNumber] forState:(UIControlStateNormal)];
// NSLog(@"%ld",_pageNumber);
}
//动态创建view
- (void)creatView:(NSInteger)number
{
BOOL isbool = [_mArraycontainsObject: [NSStringstringWithFormat:@"%ld",(long)number]];
if (isbool == 0) {
[_mArrayaddObject:[NSStringstringWithFormat:@"%ld",(long)number]];
UIView * view =[[UIViewalloc] initWithFrame:CGRectMake(number*_scrollview.frame.size.width,0,_scrollview.frame.size.width,_scrollview.frame.size.height)];
NSLog(@"creatSubv%@",_mArray);
[_scrollviewaddSubview:view];
}else{
NSLog(@"Digital already exists");
}
}