您的位置:首页 > 产品设计 > UI/UE

iOS UIPageControl与UIScrollView配合使用代码

2014-11-04 21:56 330 查看
#import "RootViewController.h"
#import "RootView.h"

@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)RootView *myview;
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.myview = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.myview.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
}
return self;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
self.myview.page.currentPage = scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width;
}
-(void)pageAction:(UIPageControl*)sender
{
[UIView animateWithDuration:0.5 animations:^{
self.myview.scrollV.contentOffset = CGPointMake(sender.currentPage*[UIScreen mainScreen].bounds.size.width, 0);
}];

}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.myview.imV;
}
-(void)loadView
{
self.view = self.myview;
self.myview.scrollV.delegate = self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end
#import <UIKit/UIKit.h>

@interface RootView : UIView
@property(nonatomic,retain)UIScrollView *scrollV;
@property(nonatomic,retain)UIImageView *imV;
@property(nonatomic,retain)UIPageControl *page;
@end


#import "RootView.h"

@implementation RootView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addAllViews];
}
return self;
}
-(void)addAllViews
{
self.scrollV = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.scrollV.backgroundColor = [UIColor yellowColor];
self.scrollV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width*8, 0);
self.scrollV.pagingEnabled = YES;
//    self.scrollV.bounces = NO;
//    self.scrollV.bouncesZoom = NO;
self.scrollV.minimumZoomScale = 0.3;
self.scrollV.maximumZoomScale = 2;
self.scrollV.zoomScale = 0.3;
[self addSubview:self.scrollV];
for (int i = 0; i < 9; i++)
{
self.imV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"h%d.jpeg",i+1]]];
self.imV.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*i, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
[self.scrollV addSubview:self.imV];

}
self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-30, [UIScreen mainScreen].bounds.size.width, 30)];
self.page.backgroundColor = [UIColor grayColor];
self.page.numberOfPages = 8;
self.page.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
self.page.pageIndicatorTintColor = [UIColor grayColor];
[self addSubview:self.page];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐