RoateView, createview

Source: Internet
Author: User

RoateView, createview

1 # import <UIKit/UIKit. h> 2 3 typedef void (^ Click) (NSInteger index); 4 5 @ interface SLRoateView: UIView 6/* 7 // The cell reuse mechanism in tableView has never been completed, so it is meaningless to put only the image in tableview, because it is necessary to create arrays so many tableviews 8 {9/ /When the View loaded by scrollView is not imageView, such as the tableView on scrollVeiw in some apps, and so on, the view used for classification display is not allowed to be carousel for movies, TVs, variety shows, and cartoons on the iQiYi homepage. and click Properties 10 11 // whether to allow carousel by default NO12 @ property (nonatomic, assign) BOOL AllowCarousel; 13 14 // you can click to determine whether the block is empty or not. Do not add a gesture 15 @ property (nonatomic, assign) BOOL suppinterinteraction; 16} 17 */18 // create 19 + (SLRoateView *) roateViewWithFrame :( CGRect) frame array :( NSArray *) array click :( Click) click; 20 21 //-(instancetype) initWithFrame :( CGRect) frame array :( NSArray *) array click :( Click) click; 22 23 @ end
# Import "SLRoateView. h "@ interface SLRoateView () <UIScrollViewDelegate> @ property (nonatomic, strong) NSArray * array; @ property (nonatomic, copy) Click click; @ property (nonatomic, assign) CGFloat KRoateWidth; @ property (nonatomic, assign) CGFloat KRoateHeight; @ property (nonatomic, strong) UIImageView * Temperature; @ property (nonatomic, strong) UIImageView * centerImageView; @ property (nonatomic, strong) UIImageView * RightImageView; @ property (nonatomic, assign) NSInteger index; @ property (nonatomic, strong) UIPageControl * pageControl; @ property (nonatomic, strong) NSTimer * timer; @ property (nonatomic, strong) UIScrollView * scroll; @ end @ implementation SLRoateView + (SLRoateView *) roateViewWithFrame :( CGRect) frame array :( NSArray *) array click :( Click) click {SLRoateView * roateView = [[SLRoateView alloc] initWithFrame: frame array: Array click: click]; return roateView;}-(instancetype) initWithFrame :( CGRect) frame array :( NSArray *) array click :( Click) click {self = [super initWithFrame: frame]; if (self) {self. KRoateWidth = frame. size. width; self. KRoateHeight = frame. size. height; self. array = array; self. click = click; // scrollVeiw sets self. scroll = [[UIScrollView alloc] initWithFrame: CGRectMake (0, 0, _ KRoateWidth, _ KRoateHeight )]; Self. scroll. contentSize = CGSizeMake (_ KRoateWidth * 3, 0); self. scroll. pagingEnabled = YES; self. scroll. contentOffset = CGPointMake (_ KRoateWidth, 0); self. scroll. alwaysBounceVertical = NO; self. scroll. showsVerticalScrollIndicator = NO; self. scroll. alwaysBounceHorizontal = YES; self. scroll. showsHorizontalScrollIndicator = NO; self. scroll. delegate = self; [self addSubview: _ scroll]; [self addGest UreRecognizer]; [self addImageViews]; [self setDefautImage]; [self addPageControl]; [self addTimer];} return self ;}# pragma mark adds a timer-(void) addTimer {// create a timer in the sub-thread as the image carousel dispatch_async (dispatch_get_global_queue (0, 0), ^ {_ timer = [nst1_timerwithtimeinterval: 3 target: self selector: @ selector (rotaView) userInfo: nil repeats: YES]; [[nsunloop currentRunLoop] addTimer: _ timer forMode: nsunloopco MmonModes]; [[nsunloop currentRunLoop] run];});}-(void) rotaView {[_ scroll setContentOffset: CGPointMake (_ KRoateWidth * 2, 0) animated: YES]; NSLog (@ "% ld", _ index); // set pagination. Because the animation is moved from the middle to the third, pageControl displays the third image instead of the middle image. currentPage = (_ index + 1) % _ array. count; if (_ scroll. contentOffset. x/_ KRoateWidth = 2) {[self reloadImage]; // scrollView returns to the middle [self. scroll setContentOffset: CGPoint Make (_ KRoateWidth, 0) animated: NO] ;}# pragma mark add Click Event-(void) addGestureRecognizer {[self addGestureRecognizer: [Export alloc] initWithTarget: self action: @ selector (tapGestureRecognizer)];}-(void) tapGestureRecognizer {if (_ click) {// use the current self. click (_ pageControl. currentPage) ;}# pragma mark add an image control-(void) addImageViews {_ leftImageView = [[UIImageView alloc] initWithFrame: CGRectM Ake (0, 0, _ KRoateWidth, _ KRoateHeight)]; // The imageView size does not change. contentMode = UIViewContentModeScaleAspectFit; [self. scroll addSubview: _ blank]; _ centerImageView = [[UIImageView alloc] initWithFrame: CGRectMake (_ KRoateWidth, 0, _ KRoateWidth, _ KRoateHeight)]; // _ centerImageView. contentMode = UIViewContentModeScaleAspectFit; [self. scrol L addSubview: _ centerImageView]; _ rightImageView = [[UIImageView alloc] initWithFrame: CGRectMake (_ KRoateWidth * 2, 0, _ KRoateWidth, _ KRoateHeight)]; // _ timeout. contentMode = UIViewContentModeScaleAspectFit; [self. scroll addSubview: _ rightImageView]; NSLog (@ "% f", _ leftImageView. frame. size. height, _ centerImageView. frame. size. height, _ rightImageView. frame. size. height) ;}# pragma mark sets the default display graph Slice-(void) setDefautImage {_ leftImageView. image = _ array [_ array. count-1]; _ centerImageView. image = _ array [0]; _ rightImageView. image = _ array [1]; _ index = 0; _ pageControl. currentPage = _ index;} # pragma mark Add a paging control-(void) addPageControl {// return the value (0, 0) based on the appropriate UIPageControl size) I don't know why ---- create it first and then give it to frame _ pageControl = [[UIPageControl alloc] init]; CGSize size = [_ pageControl sizeForNumberOfPages: _ array. count]; _ PageControl. frame = CGRectMake (0, 0, size. width, size. height); _ pageControl. center = CGPointMake (_ KRoateWidth/2, _ KRoateHeight-size. height/2); _ pageControl. numberOfPages = _ array. count; [self addSubview: _ pageControl];} // # pragma mark drag event //-(void) scrollViewDidScroll :( UIScrollView *) scrollView // {// if (scrollView. isDragging) // {// NSLog (@ "Disable timer"); // [_ timer setFireDate: [NSDate distantFuture] ]; //} // If (! ScrollView. isDragging) // {// NSLog (@ "asdfasdf"); //} // # pragma mark rolling stop event-(void) scrollViewDidEndDecelerating :( UIScrollView *) scrollView {// after 5 seconds, start the timer [_ timer mselector: @ selector (setFireDate :) withObject: [NSDate distantPast] afterDelay: 5]; [self reloadImage]; // return scrollView to the middle [self. scroll setContentOffset: CGPointMake (_ KRoateWidth, 0) animated: NO]; // set page_pagecontrol. currentPage = _ index ;}# pragma mark reload the image-(void) reloadImage {NSInteger leftIndex, rigtIndex; CGPoint offset = self. scroll. contentOffset; if (offset. x> _ KRoateWidth) {_ index = (_ index + 1) % _ array. count;} else if (offset. x <_ KRoateWidth) {_ index = (_ index + _ array. count-1) % _ array. count;} _ centerImageView. image = _ array [_ index]; leftIndex = (_ index + _ array. count-1) % _ array. count; rigtIndex = (_ index + 1) % _ array. count; _ leftImageView. image = _ array [leftIndex]; _ rightImageView. image = _ array [rigtIndex];}-(void) scrollViewWillBeginDragging :( UIScrollView *) scrollView {if (scrollView. isDragging) {// cancel the execution request before timer [NSObject cancelPreviousPerformRequestsWithTarget: _ timer]; // disable the timer [_ timer setFireDate: [NSDate distantFuture];}

 

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.