Uiscrollview control to implement picture carousel
First, the realization effect
Implement automatic carousel of pictures
Second, the implementation of the Code
Layout in Storyboard
Code:
Copy Code code as follows:
#import "YYViewController.h"
@interface Yyviewcontroller () <UIScrollViewDelegate>
@property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview;
/**
* Page number
*/
@property (Weak, nonatomic) Iboutlet Uipagecontrol *pagecontrol;
@property (nonatomic, strong) Nstimer *timer;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
The width of the picture
CGFloat Imagew = self.scrollview.frame.size.width;
CGFloat Imagew = 300;
Picture High
CGFloat Imageh = self.scrollview.frame.size.height;
The Y of the picture
CGFloat imagey = 0;
Number of pictures
Nsinteger totalcount = 5;
1. Add 5 Photos
for (int i = 0; i < TotalCount; i++) {
Uiimageview *imageview = [[Uiimageview alloc] init];
Picture X
CGFloat ImageX = i * IMAGEW;
Set frame
Imageview.frame = CGRectMake (ImageX, Imagey, Imagew, Imageh);
Set picture
NSString *name = [NSString stringwithformat:@ "img_0%d", i + 1];
Imageview.image = [UIImage imagenamed:name];
Hide Indicator Bar
Self.scrollview.showsHorizontalScrollIndicator = NO;
[Self.scrollview Addsubview:imageview];
}
2. Set the scrolling range of the ScrollView
CGFloat contentw = TotalCount *imagew;
Scrolling is not allowed in the vertical direction
Self.scrollview.contentSize = Cgsizemake (contentw, 0);
3. Set pagination
self.scrollview.pagingEnabled = YES;
4. Monitor the scrolling of ScrollView
Self.scrollview.delegate = self;
[Self addtimer];
}
-(void) nextimage
{
int page = (int) self.pageControl.currentPage;
if (page = = 4) {
page = 0;
}else
&NBSP;&NBSP;&N Bsp {
page++;
}
// scrolling scrollview
cgfloat x = page * SELF.SCROLLVIEW.FRAME.SIZE.W Idth;
self.scrollview.contentOffset = cgpointmake (x, 0);
}
ScrollView when scrolling is called
-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView
{
NSLog (@ "in scroll");
Calculate page Numbers
Page number = (Contentoffset.x + scrollview half width)/scrollview width
CGFloat scrollvieww = scrollView.frame.size.width;
CGFloat x = scrollview.contentoffset.x;
int page = (x + scrollvieww/2)/scrollvieww;
self.pageControl.currentPage = page;
}
Start dragging the call when
-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView
{
Turn off timer (note point; Once the timer is turned off, it cannot be opened again)
[Self.timer invalidate];
[Self removetimer];
}
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate
{
Open Timer
[Self addtimer];
}
/**
* Open Timer
*/
-(void) addtimer{
Self.timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (nextimage) Userinfo:nil repeats : YES];
106}
/**
* Close Timer
*/
-(void) Removetimer
{
[Self.timer invalidate];
}
@end
Tip: The following two properties have been wired to the controls in storyboard.
Copy Code code as follows:
@property (Weak, nonatomic) Iboutletuiscrollview *scrollview;
@property (Weak, nonatomic) Iboutletuipagecontrol *pagecontrol;
Add: Timer Nstimer
The timer is suitable for a longer interval of time.
Nstimeinterval: How long many pieces of operation once
Target: Who to manipulate
Selector: The method to be manipulated
UserInfo: Passing Parameters
Repeats: Do you want to repeat
Copy Code code as follows:
Self.timer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (nextimage) Userinfo:nil repeats : YES];
implement load more functionality in UITableView
First, the realization effect
Click to load more buttons, a load icon appears, three seconds after adding two new data.
II. Implementation code and description
When you click to load more buttons in the page (View section), the main page (the host controller) loads two of data in.
When the view part of the button is clicked, to let the host controller load the data, refresh the table, 2B youth will add a master controller's properties in the view, through this property to call to load, but in development usually through proxy mode to complete this operation.
The following are two implementations of code.
1. Project Structure and description
Description: Load more always placed at the bottom of this tableview, so here is set to this TableView Tablefooterview.
Copy Code code as follows:
Self.tableview.tablefooterview=footerview;
In the implementation of the processing through the xib, taking into account the left and right, and click to switch to the Load button and text, to control both the icon and text, so the load icon and text prompts in a view in order to control, This xib has been associated with Yyfooterview.xib, which controls xib by this class.
2, implementation code
(1). Garbage code
Data Model Section
YYtg.h file
Copy Code code as follows:
//
YYtg.h
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Global.h"
@interface Yytgmodel:nsobject
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *buycount;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *price;
External interface
Yyinith (TG)
@end
YYTG.M file
Copy Code code as follows:
//
Yytg.m
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYtgModel.h"
@implementation Yytgmodel
YYINITM (TG)
@end
Note: The Construction method interface and implementation code for the data Transfer Model section has been encapsulated by customizing the macros with parameters.
The package code is as follows:
Copy Code code as follows:
#ifndef _0____________global_h
#define _0____________global_h
/**
* Customize macros with parameters
*/
#define YYINITH (name)-(Instancetype) Initwithdict: (Nsdictionary *) dict;\
+ (instancetype) name# #WithDict:(nsdictionary *) dict;
#define YYINITM (name)-(Instancetype) Initwithdict: (Nsdictionary *) dict\
{\
if (Self=[super init]) {\
[Self setvaluesforkeyswithdictionary:dict];\
}\
Return self;\
}\
\
+ (instancetype) name# #WithDict:(nsdictionary *) dict\
{\
return [[Self alloc]initwithdict:dict];\
}\
#endif
View section
YYtgcell.h file
Copy Code code as follows:
//
YYtgcell.h
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "YYtgModel.h"
@interface Yytgcell:uitableviewcell
@property (Nonatomic,strong) Yytgmodel *yytg;
To encapsulate the load data (the internal details of creating a cell using Xib)
+ (Instancetype) Tgcellwithtableview: (UITableView *) TableView;
@end
YYTGCELL.M file
Copy Code code as follows:
//
yytgcell.m
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYtgcell.h"
Private extension
@interface Yytgcell ()
@property (Strong, nonatomic) Iboutlet Uiimageview *img;
@property (Strong, nonatomic) Iboutlet Uilabel *titlelab;
@property (Strong, nonatomic) Iboutlet Uilabel *pricelab;
@property (Strong, nonatomic) Iboutlet Uilabel *buycountlab;
@end
Copy Code code as follows:
@implementation Yytgcell
#pragma mark overrides the Set method to complete the assignment of the data
-(void) SETYYTG: (Yytgmodel *) YYTG
{
_yytg=yytg;
& nbsp; Self.img.image=[uiimage ImageNamed:yytg.icon];
Self.titlelab.text=yytg.title;
self.pricelab.text=[nsstring stringwithformat:@ "$%@", Yytg.price];
self.buycountlab.text=[nsstring stringwithformat:@ "have been purchased by%@ people", Yytg.buycount];
}
+ (Instancetype) Tgcellwithtableview: (UITableView *) TableView
{
static NSString *identifier= @ "TG";
Yytgcell *cell=[tableview Dequeuereusablecellwithidentifier:identifier];
if (Cell==nil) {
How to add a stamp to the created cell
For loaded xib files, you can set them in the property selector of the Xib view
Cell=[[[nsbundle mainbundle]loadnibnamed:@ "Tgcell" Owner:nil Options:nil]firstobject];
NSLog (@ "created a cell");
}
return cell;
}
@end
YYfooterview.h file
Copy Code code as follows:
//
YYfooterview.h
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <UIKit/UIKit.h>
@class Yyviewcontroller;
@interface Yyfooterview:uiview
@property (Nonatomic,strong) Yyviewcontroller *controller;
@end
YYFOOTERVIEW.M file
Copy Code code as follows:
//
yytgcell.m
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYtgcell.h"
Private extension
@interface Yytgcell ()
@property (Strong, nonatomic) Iboutlet Uiimageview *img;
@property (Strong, nonatomic) Iboutlet Uilabel *titlelab;
@property (Strong, nonatomic) Iboutlet Uilabel *pricelab;
@property (Strong, nonatomic) Iboutlet Uilabel *buycountlab;
@end
Copy Code code as follows:
@implementation Yytgcell
#pragma mark overrides the Set method to complete the assignment of the data
-(void) SETYYTG: (Yytgmodel *) YYTG
{
_yytg=yytg;
& nbsp; Self.img.image=[uiimage ImageNamed:yytg.icon];
Self.titlelab.text=yytg.title;
self.pricelab.text=[nsstring stringwithformat:@ "$%@", Yytg.price];
self.buycountlab.text=[nsstring stringwithformat:@ "have been purchased by%@ people", Yytg.buycount];
}
+ (Instancetype) Tgcellwithtableview: (UITableView *) TableView
{
static NSString *identifier= @ "TG";
Yytgcell *cell=[tableview Dequeuereusablecellwithidentifier:identifier];
if (Cell==nil) {
How to add a stamp to the created cell
For loaded xib files, you can set them in the property selector of the Xib view
Cell=[[[nsbundle mainbundle]loadnibnamed:@ "Tgcell" Owner:nil Options:nil]firstobject];
NSLog (@ "created a cell");
}
return cell;
}
@end
YYfooterview.h file
Copy Code code as follows:
//
YYfooterview.h
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <UIKit/UIKit.h>
@class Yyviewcontroller;
@interface Yyfooterview:uiview
@property (Nonatomic,strong) Yyviewcontroller *controller;
@end
YYFOOTERVIEW.M file
Copy Code code as follows:
//
yyfooterview.m
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYfooterview.h"
#import "YYViewController.h"
@interface Yyfooterview ()
@property (Strong, nonatomic) Iboutlet Uiactivityindicatorview *loadingview;
@property (Strong, nonatomic) Iboutlet UIButton *loadbtn;
@end
Copy Code code as follows:
@implementation Yyfooterview
-(ibaction) Loadbtclick {
; NSLog (@ "button was clicked");
//Hide buttons
self.loadbtn.hidden=yes;
//show chrysanthemum
self.loadingview.hidden=no;
#warning simulate sending a network request, after 3 seconds to hide Daisy
Dispatch_after (Dispatch_time Dispatch _time_now, (int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
//3. Call control load Data method
[Self.controller Loadmore];
//4. Hide Daisy View
Self.loadingview.hidden = YES;
//5. Display Buttons
Self.loadbtn.hidden = NO;
});
}
@end
Master Controller
YYViewController.h file
Copy Code code as follows:
//
YYViewController.h
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Yyviewcontroller:uiviewcontroller
public interface
-(void) Loadmore;
@end
YYVIEWCONTROLLER.M file
Copy Code code as follows:
//
Yyviewcontroller.m
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYViewController.h"
#import "YYtgModel.h"
#import "YYtgcell.h"
#import "YYfooterview.h"
@interface Yyviewcontroller () <UITableViewDataSource,UITableViewDelegate>
@property (Strong, nonatomic) Iboutlet UITableView *tableview;
@property (strong,nonatomic) Nsmutablearray *TG;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
#pragma mark-Loading Data method
-(void) Loadmore
{
Creating a Model
Yytgmodel *tgmodel=[[yytgmodel Alloc]init];
tgmodel.title=@ "Dishes good on the table";
tgmodel.icon=@ "5ee372ff039073317a49af5442748071";
tgmodel.buycount=@ "20";
tgmodel.price=@ "10000";
To add a model to an array
[Self.tg Addobject:tgmodel];
Yytgmodel *tgmodelq=[[yytgmodel Alloc]init];
tgmodelq.title=@ "dish good on table 1";
tgmodelq.icon=@ "5ee372ff039073317a49af5442748071";
tgmodelq.buycount=@ "20";
tgmodelq.price=@ "10000";
[Self.tg ADDOBJECT:TGMODELQ];
Refresh Table
[Self.tableview Reloaddata];
}
-(void) viewdidload
{
[Super Viewdidload];
SELF.TABLEVIEW.ROWHEIGHT=80.F;
Load Bottom View
Getting the data from the Xib
uinib *nib=[uinib nibwithnibname:@ "Yyfooterview" bundle:nil];
Yyfooterview *footerview=[[nib Instantiatewithowner:nil Options:nil] firstobject];
Self.tableview.tablefooterview=footerview;
Setting control
footerview.controller=self;
}
#pragma mark-lazy Load
-(Nsarray *) TG
{
if (_tg==nil) {
NSString *fullpath=[[nsbundle mainbundle]pathforresource:@ "Tgs.plist" oftype:nil];
Nsarray *temparray=[nsarray Arraywithcontentsoffile:fullpath];
Nsmutablearray *arraym=[nsmutablearray ArrayWithCapacity:temparray.count];
For (Nsdictionary *dict in Temparray) {
Yytgmodel *tg=[yytgmodel Tgwithdict:dict];
[Arraym ADDOBJECT:TG];
}
_tg=arraym;
}
return _TG;
}
#pragma mark-xib to create cell data processing
How many groups #pragma mark?
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return 1;
}
How many lines #pragma mark?
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return self.tg.count;
}
#pragma mark sets each row for each group
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
1. Create a cell
Yytgcell *cell=[yytgcell Tgcellwithtableview:tableview];
2. Get the model of the current row, set the data for the cell
Yytgmodel *tg=self.tg[indexpath.row];
CELL.YYTG=TG;
3. Return cell
return cell;
}
#pragma mark-Hide Status bar
-(BOOL) Prefersstatusbarhidden
{
return YES;
}
@end
2. Through the agent to complete
When the button is clicked, the View section itself does not work, but notifies its agent (Controller) to complete the next action.
This section of code modifies the following files on a 1 basis:
View section:
YYfooterview.h file
Copy Code code as follows:
//
YYfooterview.h
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <UIKit/UIKit.h>
@class Yyviewcontroller, Yyfooterview;
Contract Agreement
@protocol yyfooterviewdelegate <NSObject>
-(void) Footerviewloadmore;
@end
@interface Yyfooterview:uiview
Declares an ID type attribute that adheres to the "person" of the protocol to become its proxy
@property (Nonatomic,strong) id<yyfooterviewdelegate> delegate;
//@property (Nonatomic,strong) Yyviewcontroller *controller;
@end
YYFOOTERVIEW.M file
Copy Code code as follows:
//
yyfooterview.m
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYfooterview.h"
#import "YYViewController.h"
@interface Yyfooterview ()
@property (Strong, nonatomic) Iboutlet Uiactivityindicatorview *loadingview;
@property (Strong, nonatomic) Iboutlet UIButton *loadbtn;
@end
Copy Code code as follows:
@implementation Yyfooterview
-(ibaction) Loadbtclick {
NSLog (@ "button was clicked");
Hide button
Self.loadbtn.hidden=yes;
Show Chrysanthemum
Self.loadingview.hidden=no;
#warning simulate sending a network request, hide the chrysanthemum after 3 seconds
Dispatch_after (Dispatch_time (Dispatch_time_now, int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
3. Call control Load Data method
[Self.controller Loadmore];
Notification Agent
[Self.delegate Footerviewloadmore];
4. Hide the daisy View
Self.loadingview.hidden = YES;
5. Display button
Self.loadbtn.hidden = NO;
});
}
@end
Main Controller Section
YYViewController.h file
Copy Code code as follows:
//
YYViewController.h
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Yyviewcontroller:uiviewcontroller
public interface
-(void) Loadmore;
@end
YYVIEWCONTROLLER.M file
Copy Code code as follows:
//
Yyviewcontroller.m
02-Group Purchase (use Xib and class to complete data display)
//
Created by Apple on 14-5-29.
Copyright (c) 2014 itcase. All rights reserved.
//
#import "YYViewController.h"
#import "YYtgModel.h"
#import "YYtgcell.h"
#import "YYfooterview.h"
@interface Yyviewcontroller () <UITableViewDataSource,UITableViewDelegate,YYfooterviewDelegate>
@property (Strong, nonatomic) Iboutlet UITableView *tableview;
@property (strong,nonatomic) Nsmutablearray *TG;
@end
Copy Code code as follows:
@implementation Yyviewcontroller
#pragma mark-Loading Data method
-(void) Footerviewloadmore
{
Creating a Model
Yytgmodel *tgmodel=[[yytgmodel Alloc]init];
tgmodel.title=@ "Dishes good on the table";
tgmodel.icon=@ "5ee372ff039073317a49af5442748071";
tgmodel.buycount=@ "20";
tgmodel.price=@ "10000";
To add a model to an array
[Self.tg Addobject:tgmodel];
Yytgmodel *tgmodelq=[[yytgmodel Alloc]init];
tgmodelq.title=@ "dish good on table 1";
tgmodelq.icon=@ "5ee372ff039073317a49af5442748071";
tgmodelq.buycount=@ "20";
tgmodelq.price=@ "10000";
[Self.tg ADDOBJECT:TGMODELQ];
Refresh Table
[Self.tableview Reloaddata];
}
-(void) Loadmore
//{
Creating a Model
Yytgmodel *tgmodel=[[yytgmodel Alloc]init];
tgmodel.title=@ "Dishes good on the table";
tgmodel.icon=@ "5ee372ff039073317a49af5442748071";
tgmodel.buycount=@ "20";
tgmodel.price=@ "10000";
To add a model to an array
[Self.tg Addobject:tgmodel];
//
Yytgmodel *tgmodelq=[[yytgmodel Alloc]init];
tgmodelq.title=@ "dish good on table 1";
tgmodelq.icon=@ "5ee372ff039073317a49af5442748071";
tgmodelq.buycount=@ "20";
tgmodelq.price=@ "10000";
//
[Self.tg ADDOBJECT:TGMODELQ];
Refresh Table
[Self.tableview Reloaddata];
//}
-(void) viewdidload
{
[Super Viewdidload];
SELF.TABLEVIEW.ROWHEIGHT=80.F;
Load Bottom View
Getting the data from the Xib
uinib *nib=[uinib nibwithnibname:@ "Yyfooterview" bundle:nil];
Yyfooterview *footerview=[[nib Instantiatewithowner:nil Options:nil] firstobject];
Self.tableview.tablefooterview=footerview;
Setting control
footerview.controller=self;
Set up agents
footerview.delegate=self;
}
#pragma mark-lazy Load
-(Nsarray *) TG
{
if (_tg==nil) {
NSString *fullpath=[[nsbundle mainbundle]pathforresource:@ "Tgs.plist" oftype:nil];
Nsarray *temparray=[nsarray Arraywithcontentsoffile:fullpath];
Nsmutablearray *arraym=[nsmutablearray ArrayWithCapacity:temparray.count];
For (Nsdictionary *dict in Temparray) {
Yytgmodel *tg=[yytgmodel Tgwithdict:dict];
[Arraym ADDOBJECT:TG];
}
_tg=arraym;
}
return _TG;
}
#pragma mark-xib to create cell data processing
How many groups #pragma mark?
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{
return 1;
}
How many lines #pragma mark?
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return self.tg.count;
}
#pragma mark sets each row for each group
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
1. Create a cell
Yytgcell *cell=[yytgcell Tgcellwithtableview:tableview];
2. Get the model of the current row, set the data for the cell
Yytgmodel *tg=self.tg[indexpath.row];
CELL.YYTG=TG;
3. Return cell
return cell;
}
#pragma mark-Hide Status bar
-(BOOL) Prefersstatusbarhidden
{
return YES;
}
@end