Use UIScrollView to switch between several pages.

Source: Internet
Author: User

Use UIScrollView to switch between several pages.

This example shows how to use UIScrollView and the table and page Jump content;

Original Author address: http://www.cocoachina.com/bbs/read.php? Tid = 323514

As follows:

 

1: use of the knowledge point rolling View

# Import "YCView. h "@ interface ViewController () <strong> @ property (nonatomic, strong) UIScrollView * scrollV; @ property (weak, nonatomic) IBOutlet UIButton * usesbtn; @ property (weak, nonatomic) IBOutlet UIButton * partBtn; @ property (weak, nonatomic) IBOutlet UIButton * serverBtn; @ end @ implementation ViewController // lazy loading-(UIScrollView *) scrollV {if (! _ ScrollV) {_ scrollV = [[UIScrollView alloc] init]; // set the frame CGFloat scrollX = 0; CGFloat scrollY = 110; CGFloat scrollW = CGRectGetWidth (self. view. bounds); CGFloat scrollH = CGRectGetHeight (self. view. bounds); _ scrollV. frame = CGRectMake (scrollX, scrollY, scrollW, scrollH); // sets proxy _ scrollV. delegate = self; // Add the scrollView to the view of the controller [self. view addSubview: _ scrollV];} return _ scrollV ;}-( Void) viewDidLoad {[super viewDidLoad]; // Add view [self addScrollView]; self. scrollV. contentOffset = CGPointMake (0, 0);}-(void) addScrollView {// Add 3 views for (int I = 0; I <3; I ++) {CGFloat viewX = I * [UIScreen mainScreen]. bounds. size. width; CGFloat viewY = 0; CGFloat viewW = [UIScreen mainScreen]. bounds. size. width; CGFloat viewH = [UIScreen mainScreen]. bounds. size. height-108; YCView * v = [[ YCView alloc] initWithFrame: CGRectMake (viewX, viewY, viewW, viewH)]; v. backgroundColor = [UIColor colorWithRed: arc4random_uniform (255)/255.0 green: arc4random_uniform (255)/255.0 blue: arc4random_uniform (255)/255.0 alpha: 1.0]; [self. scrollV addSubview: v];} // sets the frame, offset // sets the page self. scrollV. pagingEnabled = YES; self. scrollV. backgroundColor = [UIColor orangeColor]; // sets the scroll range self. scrollV. contentSize = CGSizeMake ([UIScreen mainScreen]. bounds. size. width * 3, [UIScreen mainScreen]. bounds. size. height); // sets the offset self. scrollV. contentOffset = CGPointMake ([UIScreen mainScreen]. bounds. size. width, 0); // cancel the spring effect self of scrollView scrolling to the edge. scrollV. bounces = NO; // hide the horizontal scroll bar self. scrollV. showsHorizontalScrollIndicator = NO;} # pragma mark -- UIScrollViewDelegate-(void) scrollViewDidScroll :( UIScrollView *) scrollView {/ /Set the color of scrollView. contentOffset. x = 0? [Self. usesbtn setTitleColor: [UIColor orangeColor] forState: UIControlStateNormal]: [self. usesbtn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; scrollView. contentOffset. x = ([UIScreen mainScreen]. bounds. size. width )? [Self. partBtn setTitleColor: [UIColor orangeColor] forState: UIControlStateNormal]: [self. partBtn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; scrollView. contentOffset. x = ([UIScreen mainScreen]. bounds. size. width) * 2? [Self. serverBtn setTitleColor: [UIColor orangeColor] forState: UIControlStateNormal]: [self. serverBtn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];} # pragma mark -- btnClick-(IBAction) usesBtnClick :( id) sender {// jump to 1st view contentOffset. x = screen width * 0 // reset the scrollView position [UIView animateWithDuration: 0.5 animations: ^ {self. scrollV. contentOffset = [self ScrollViewWithContentOffSetPage: 0];}-(IBAction) partBtnClick :( id) sender {// jump to 2nd view contentOffset. x = screen width * 1 // reset the scrollView position [UIView animateWithDuration: 0.5 animations: ^ {self. scrollV. contentOffset = [self ScrollViewWithContentOffSetPage: 1];}-(IBAction) serverBtnClick :( id) sender {// jump to 3rd view contentOffset. x = screen width * 2 // reset the scrollView position [UIView animateWithDuration: 0.5 animations: ^ {self. scrollV. contentOffset = [self ScrollViewWithContentOffSetPage: 2];} // returns the scrollView offset-(CGPoint) offset :( NSInteger) page {return CGPointMake ([UIScreen mainScreen]. bounds. size. width) * page, 0);}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end

 

2: List and jump display content

# Import "YCView. h "# import" YCCellView. h "static NSString * idenifer = @" YCCollectionViewCell "; # define CellWeigth ([UIScreen mainScreen]. bounds. size. width)/3.0 # define CellHeigth 44 @ interface YCView () <UITableViewDataSource, UITableViewDelegate> @ property (strong, nonatomic) NSArray * parts; @ property (strong, nonatomic) NSMutableArray * Views; @ end @ implementation YCView // lazy loading-(NSMutableArray *) Views {if (! _ Views) {_ Views = [NSMutableArray array];} return _ Views;} // lazy loading-(NSArray *) parts {if (! _ Parts) {_ parts = [NSArray array]; _ parts = @ [@ "Popular recommendations", @ "Car exterior decoration", @ "perfume/purification ", @ "functional supplies", @ "beauty maintenance", @ "Security/Protection", @ "audio/video navigation"];} return _ parts;}-(instancetype) init {if (self = [super init]) {[self addView];} return self;}-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {[self addView];} return self;}-(void) addView {// Add tableView UITableView * tableView = [[UITableView alloc] initWithFrame: CGRectMake (0, -44, CellWeigth, [UIScreen mainScreen]. bounds. size. height) style: UITableViewStyleGrouped]; tableView. backgroundColor = [UIColor redColor]; tableView. dataSource = self; tableView. delegate = self; [self addSubview: tableView] ;}# pragma mark -- UITableViewDataSource-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. parts. count;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * ID = @ "YCCell"; UITableViewCell * cell = [tableView preview: ID]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: ID];} cell. textLabel. text = self. parts [indexPath. row]; return cell ;}# pragma mark -- UITableViewDelegate-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {[self addBestView];}-(void) addBestView {YCCellView * view = [[YCCellView alloc] initWithFrame: CGRectMake (CellWeigth, 0, ([UIScreen mainScreen]. bounds. size. width)-CellWeigth, [UIScreen mainScreen]. bounds. size. height)]; view. backgroundColor = [UIColor redColor]; [[self. views lastObject] removeFromSuperview]; [self. views addObject: view]; [self addSubview: view];}-(void) layoutSubviews {[super layoutSubviews];} @ end

3: content of the cell column

#import "YCCellView.h"#define ViewMagin 10#define ViewHeight 90#define ViewWeight (([UIScreen mainScreen].bounds.size.width)-CellWeigth - 3*ViewMagin)/3.0#define CellWeigth ([UIScreen mainScreen].bounds.size.width)/3.0@interface YCCellView ()@end@implementation YCCellView- (instancetype)init{    if(self = [super init])    {        [self addCollectionView];    }    return self;}- (instancetype)initWithFrame:(CGRect)frame{    if(self = [super initWithFrame:frame])    {        [self addCollectionView];    }    return self;}- (void)addCollectionView{    for(int i = 0; i < 4; i++)    {        for(int j = 0; j < 3; j++)        {            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(j * (ViewWeight + ViewMagin), i * (ViewHeight + ViewMagin), ViewWeight, ViewHeight)];            v.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/ 255.0 green:arc4random_uniform(255)/ 255.0 blue:arc4random_uniform(255)/ 255.0 alpha:1.0];            [self addSubview:v];        }    }}@end

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.