iOS Control Encapsulation-(uiscrollview) Picture Carousel

Source: Internet
Author: User

First look at the effect:

File One: PictureCarouselView.h

Import define Self_width self.scrollView.frame.size.widthdefine self_height self.scrollView.frame.size.heightimport "PictureCarouselView.h"

@interface Picturecarouselview ()
@property (Weak, nonatomic) Iboutlet Uiscrollview *scrollview;
@property (Weak, nonatomic) Iboutlet Uipagecontrol *pagecontrol;
@property (Strong, nonatomic) Nsmutablearray *picturebuttons;
@property (Strong, nonatomic) Nstimer *timer;
@property (Assign, nonatomic) BOOL isautomatic;
@property (Assign, nonatomic) BOOL isanimation;
@property (assign, nonatomic) float timeinterval;
@end

@implementation Picturecarouselview

pragma mark-Attribute method
    • (void) Setpictures: (Nsarray *) pictures
      {
      if (Pictures) {
      _pictures = pictures;
      [Self setimageviewswithpictures:pictures];
      Self.pageControl.numberOfPages = Pictures.count;
      }
      }
    • (Nsmutablearray *) picturebuttons
      {
      if (!_picturebuttons) {
      _picturebuttons = [[Nsmutablearray alloc] initwithcapacity:1];
      }

      return _picturebuttons;
      }

    • (void) setpicturesadjustshighlighted: (BOOL) picturesadjustshighlighted
      {
      _picturesadjustshighlighted = picturesadjustshighlighted;
      if (self.picturebuttons) {
      UIButton button = (UIButton ) self.picturebuttons[1];
      button.adjustsimagewhenhighlighted = picturesadjustshighlighted;
      }
      }
Pragma mark-Custom method

/**
* Method of creation and initialization
*
* @param Frame Control size
*
* @return the control itself
*/
+ (Instancetype) Picturecarouselviewwithframe: (CGRect) frame
{
Picturecarouselview *picturecarouselview = [self getxib];
Picturecarouselview.frame = frame;
return picturecarouselview;
}
/**
* Get the Xib file in the main package
*
* @return the instance object corresponding to this xib file
*/
+ (Picturecarouselview *) getxib
{
Picturecarouselview *picturecarouselview = [[[NSBundle Mainbundle] loadnibnamed:@ "Picturecarouselview" Owner:nil Options:nil] Firstobject];
return picturecarouselview;
}
/**
* Set Picture
*
* @param pictures Image Array
*/
-(void) Setimageviewswithpictures: (Nsarray *) pictures
{
To achieve the loop effect, expand two pictures
int count = Pictures.count + 2;
for (int index = 0; index < count; index++) {
Use the Nsnull placeholder to have the button create again when it is used
[Self.picturebuttons addobject:[nsnull Null]];
}
Self.scrollView.contentSize = Cgsizemake (Self_width*count, self_height);
Self.scrollView.contentOffset = Cgpointmake (self_width, 0);
}
/**
* Create the button under the corresponding page number, and make the corresponding settings
*
* @param page number
*/
-(void) Loadscrollviewwithpage: (int) page
{
UIButton *button = Self.picturebuttons[page];
if ([Button Iskindofclass:[nsnull class]]) {
button = [UIButton buttonwithtype:uibuttontypecustom];
Button.frame = CGRectMake (self_width*page, 0, Self_width, self_height);
[Self.picturebuttons replaceobjectatindex:page withobject:button];//replaces the object in the array with the page number as a UIButton
if (page = = 0) {
page = self.pictures.count-1;
}
else if (page = = self.picturebuttons.count-1)
{
page = 0;
}
Else
{
page = page-1;
}
[Button Setbackgroundimage:self.pictures[page] forstate:uicontrolstatenormal];
button.adjustsimagewhenhighlighted = self.picturesadjustshighlighted;
[Button addtarget:self action: @selector (didbuttonclicked:) forcontrolevents:uicontroleventtouchupinside];
[Self.scrollview Addsubview:button];
}
}
/**
* Set whether the control automatically scrolls
*
* @param whether the isautomatic automatically scrolls
* @param isanimation whether there is a scrolling animation
* @param timeinterval Animation time interval
*/
-(void) isautomaticdragging: (bool) isautomatic withanimation: (bool) isanimation Withtimeinterval: (float) TimeInterval
{
if (isautomatic) {
if (!self.isautomatic) {
Self.isautomatic = isautomatic;
Self.isanimation = isanimation;
Self.timeinterval = TimeInterval;
}
Self.timer = [Nstimer scheduledtimerwithtimeinterval:timeinterval target:self selector: @selector (nextPage:) userInfo : [NSNumber numberwithbool:isanimation] repeats:yes];
[[Nsrunloop Currentrunloop] AddTimer:self.timer formode:nsrunloopcommonmodes];
}
}
/**
* When scrolling automatically, scroll to the next page required action
*
* @param timer to control automatic scrolling
*/
-(void) NextPage: (Nstimer *) timer
{
[Self.scrollview Setcontentoffset:cgpointmake (self_width* (Self.pageControl.currentPage + 2), 0) animated:[ Timer.userinfo Boolvalue]];
}
/**
* Stop scrolling The actions performed
*/
-(void) scrollviewfrommovetostatic
{
int page = Self.scrollview.contentoffset.x/self_width;
if (page = = 0) {
[Self loadscrollviewwithpage:self.picturebuttons.count-2];
Self.scrollView.contentOffset = Cgpointmake (self_width* (self.picturebuttons.count-2), 0);
}
else if (page = = self.picturebuttons.count-1)
{
Self.scrollView.contentOffset = Cgpointmake (self_width, 0);
}
}
/**
* Settings for small dots in controls
*
* @param frame size
* @param alignmentmethod Alignment
* @param currentcolor The color of the current point
* @param indicatorcolor color of other dots
*/
-(void) Setpagecontrolwithframe: (CGRect) frame Alignmentmethod: (alignmentmethods) Alignmentmethod WithCurrentColor :(Uicolor) CurrentColor Withindicatorcolor: (Uicolor) Indicatorcolor
{
Self.pageControl.frame = frame;
Modify the color and right alignment of the Uipagecantrol
Self.pageControl.pageIndicatorTintColor = Indicatorcolor;
Self.pageControl.currentPageIndicatorTintColor = CurrentColor;
if (Alignmentmethod = = Alignmentmethodcenter) {
Return
}
Else
{
Nsinteger count = self.pageControl.numberOfPages;
Cgsize pointsize = [Self.pagecontrol sizefornumberofpages:count];
CGFloat page_x =-(self.pagecontrol.bounds.size.width-pointsize.width)/2;
if (Alignmentmethod = = Alignmentmethodleft) {
[Self.pagecontrol Setbounds:cgrectmake (page_x, SELF.PAGECONTROL.BOUNDS.ORIGIN.Y, Self.pageControl.bounds.size.width, Self.pageControl.bounds.size.height)];
}
else if (Alignmentmethod = = alignmentmethodright)
{
[Self.pagecontrol Setbounds:cgrectmake (-page_x, SELF.PAGECONTROL.BOUNDS.ORIGIN.Y, Self.pageControl.bounds.size.width, Self.pageControl.bounds.size.height)];
}
}
}

pragma mark-click Touch events
    • (void) didbuttonclicked: (UIButton *) sender
      {
      if (self.delegate) {
      [Self.delegate didclickedpicturecarouselview:self pictureIndex:self.pageControl.currentPage];
      }
      }
Pragma mark-uiscrollviewdelegate proxy method

Called when the user starts dragging
-(void) scrollviewwillbegindragging: (Uiscrollview *) ScrollView
{
/**
* When the user drags, the empty timer
*/
[Self.timer invalidate];
Self.timer = nil;
}
Called when scrolling to a location
-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView
{
int page = Floor ((self.scrollview.contentoffset.x + self_width/2)/self_width);
if (Page < 0| | PAGE&GT;SELF.PICTUREBUTTONS.COUNT-1) {
Return
}
if (Page > 0 && page < self.picturebuttons.count-1) {
Drag to the left to load the next page
if (Self.scrollview.contentoffset.x > Page*self_width) {
[Self loadscrollviewwithpage:page + 1];
}
Load Current page
else if (self.scrollview.contentoffset.x = = page*self_width)
{
[Self loadscrollviewwithpage:page];
}
Drag to the right to load the previous page
Else
{
[Self loadscrollviewwithpage:page-1];
}
}
if (page = = 0) {
page = self.picturebuttons.count-1;
}
else if (page = = self.picturebuttons.count-1)
{
page = 1;
}
Self.pageControl.currentPage = page-1;
}
Called when the user ends the drag
-(void) scrollviewdidenddecelerating: (Uiscrollview *) ScrollView
{
[Self scrollviewfrommovetostatic];
}
Scrolling animation is executed when the code changes, i.e. when the setcontentoffset changes
-(void) Scrollviewdidendscrollinganimation: (Uiscrollview *) ScrollView
{
[Self scrollviewfrommovetostatic];
}
Complete the drag and drop
-(void) scrollviewdidenddragging: (Uiscrollview *) ScrollView willdecelerate: (BOOL) decelerate
{
if (self.isautomatic) {
[Self isAutomaticDragging:self.isAutomatic withAnimation:self.isAnimation withTimeInterval:self.timeInterval];
}
}

@end

The corresponding Xib file:

The corresponding control uses the code:
-(void) Viewdidload {
[Super Viewdidload];

Nsmutablearray *pictures = [[Nsmutablearray alloc] initwithcapacity:1];for (int index = 0; index < 5; index++) {UII    Mage *image = [UIImage imagenamed:[nsstring stringwithformat:@ "img_%02d", (index + 1)]; [Pictures addobject:image];} Picturecarouselview *picturecarouselview = [Picturecarouselview picturecarouselviewwithframe:cgrectmake (0, 64, Screen_width, screen_width*260/600)];p icturecarouselview.pictures = pictures; [Picturecarouselview isautomaticdragging:yes withanimation:yes withtimeinterval:4];p icturecarouselview.delegate = Self;picturecarouselview.tag = 0; [Picturecarouselview Setpagecontrolwithframe:cgrectmake (Ten, Picturecarouselview.frame.size.height-22,                            PICTURECAROUSELVIEW.FRAME.SIZE.WIDTH-20, Alignmentmethod:alignmentmethodleft Withcurrentcolor:[uicolor Redcolor] Withindicatorcolor:[uicolor Graycolor]]; [Self.view Addsubview:picturecarouselview]; Picturecarouselview *picturecarouselview1= [Picturecarouselview picturecarouselviewwithframe:cgrectmake (0, + pictureCarouselView.frame.size.height, screen _width, screen_width*260/600)];p icturecarouselview1.pictures = [[pictures reverseobjectenumerator] allObjects]; [PictureCarouselView1 isautomaticdragging:no withanimation:no withtimeinterval:0];p icturecarouselview1.delegate = Self;picturecarouselview1.tag = 1; [PictureCarouselView1 Setpagecontrolwithframe:cgrectmake (Ten, Picturecarouselview.frame.size.height-22,                             PICTURECAROUSELVIEW.FRAME.SIZE.WIDTH-20, Alignmentmethod:alignmentmethodright Withcurrentcolor:[uicolor Greencolor] Withindicatorcolor:[uicolor graycolor]];[ Self.view Addsubview:picturecarouselview1];

}

Pragma mark-picturecarouselviewdelegate method
    • (void) Didclickedpicturecarouselview: (Picturecarouselview *) Picturecarouslview Pictureindex: (NSInteger) index
      {
      if (Picturecarouslview.tag = = 0) {
      Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "hint"
      Message:[nsstring stringwithformat:@ "You clicked on the%i picture! ", index + 1]
      Delegate:nil cancelbuttontitle:@ "OK"
      Otherbuttontitles:nil, nil];
      [Alertview show];
      }
      else if (Picturecarouslview.tag = = 1)
      {
      Uiviewcontroller *nextctr = [[Uiviewcontroller alloc] init];
      NextCtr.view.backgroundColor = [Uicolor colorwithred:index/5.0 green:index/5.0 blue:index/5.0 alpha:1];
      [Self.navigationcontroller pushviewcontroller:nextctr Animated:yes];
      }
      }

Code:
Code download please poke here.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS Control Encapsulation-(uiscrollview) Picture Carousel

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.