// 传入frame和装有网址的数组,  滚动图片总数

2019-11-26 作者:联系我们   |   浏览(200)

UIScrollView之轮转图片,uiscrollview轮转

在iOS开发中,经常会在APP首页看到多张图片进行轮换。刚开始做的时候,感觉很麻烦,不是很好做,查阅资料后,我总结了一下,自己封装了一个简单的轮转图片库;

UIScrollView无限滑动 ,只需要三个View,左视图,中视图,右视图。无论向左滑动,还是向右滑动,都显示中间的一个View; 

( _scrollView.contentOffset =CGPointMake(scrollView.bounds.size.width,0); )

 

 

//  CircleScrollView.h

 

#import <UIKit/UIKit.h>

#import "UIImageView+WebCache.h" // 当使用网络图片的时候,才会用到SDWebImage

@protocol CircleScrollViewDelegate <NSObject>

@optional

/**

 *  滚动图片总数

 *  @return 要滚动的图片的个数

 */

  • (NSInteger) numberOfImagesInScrollView;

 

/**

 *  返回索引位置的图片,仅当滚动的为图片时使用

 *  @param index 索引位置

 *  @return 索引位置的图片

 */

-(UIImage *)imageForIndex:(NSInteger) index;

 

/**

 *  返回索引位置的图片URL,仅当滚动的为图片时使用

 *  @param index 索引位置

 *  @return 索引位置对应的图片URL

 */

-(NSURL *)imageURLForIndex:(NSInteger)index;

 

/**

 *  选中相应的视图的操作

 *  @param index 选中的相应的视图

 */

  • (void)didSelectAtIndex:(NSInteger) index;

 

@end

 

@interface CircleScrollView :UIView <UIScrollViewDelegate>

 

@property (nonatomic,strong,readonly)UIScrollView *scrollView; 

@property (nonatomic,strong,readonly)UIPageControl *pageControl;

@property (nonatomic,strong,readonly)UIImageView *leftView;

@property (nonatomic,strong,readonly)UIImageView *middleView;

@property (nonatomic,strong,readonly)UIImageView *rightView;

@property (nonatomic,assign)int currentPageNo;

 

@property (nonatomic,assign,getter=isAutoLoop)BOOL autoLoop;//是否自动轮动

//@property (nonatomic,copy) NSArray *scrollImgs; 

@property (nonatomic,weak)id<CircleScrollViewDelegate> delegate;//代理

@end

 

 

//  CircleScrollView.m

 

#import "CircleScrollView.h"

@interfaceCircleScrollView ()

{

    NSTimer *_timer;

}

@end

 

@implementation CircleScrollView

/*

    自定义的ScrollView。功能如下:

    循环滑动

    使用分两步:

 (1)创建数据源(数据源中存放要滚动的图片)

 (2)_circleScrollView = [[CircleScrollView alloc] initWithFrame:CGRectMake(20, 20, 128, 128)];

 (3)设置代理 _circleScrollView.delegate = self;

 */

-(instancetype)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    if (self) {

        _scrollView = [[UIScrollViewalloc]initWithFrame:self.bounds];

        _scrollView.delegate =self;

        _scrollView.showsHorizontalScrollIndicator =NO;

        

        _pageControl = [[UIPageControlalloc]initWithFrame:CGRectMake(0,self.frame.size.height-10,self.frame.size.width,10)];

        _pageControl.backgroundColor = [UIColorlightGrayColor];

        _pageControl.pageIndicatorTintColor = [UIColorredColor];

        _pageControl.currentPageIndicatorTintColor = [UIColorblueColor];

        _pageControl.alpha =0.5;

        

        _leftView = [[UIImageViewalloc]initWithFrame:CGRectMake(self.frame.size.width*0,0,self.frame.size.width,self.frame.size.height)];

        

        _middleView = [[UIImageViewalloc]initWithFrame:CGRectMake(self.frame.size.width*1,0,self.frame.size.width,self.frame.size.height)];

        

        _rightView = [[UIImageViewalloc]initWithFrame:CGRectMake(self.frame.size.width*2,0,self.frame.size.width,self.frame.size.height)];

        

        [_scrollViewaddSubview:_leftView];

        [_scrollViewaddSubview:_middleView];

        [_scrollViewaddSubview:_rightView];

        

        _scrollView.pagingEnabled =YES;

        _scrollView.contentSize =CGSizeMake(self.frame.size.width*3,self.frame.size.height);

        _scrollView.contentOffset =CGPointMake(self.frame.size.width,0);

        

        [selfaddSubview:_scrollView];

        [selfaddSubview:_pageControl];

    }

    returnself;

}

 

-(void)setDelegate:(id<CircleScrollViewDelegate>)delegate

{

//    NSLog(@"设置代理");

    _delegate = delegate;

    if ([_delegaterespondsToSelector:@selector(numberOfImagesInScrollView)]) {

        NSInteger number = [_delegatenumberOfImagesInScrollView];

        _pageControl.numberOfPages = number;

        _pageControl.currentPage =0;

        _currentPageNo =0;

        

//        _leftView.image = [_delegate imageForIndex:number-1];

//        _middleView.image = [_delegate imageForIndex:_currentPageNo];

        int nextNo =_currentPageNo;

        if (number >2) {

            nextNo = _currentPageNo+1;

        }

//        _rightView.image = [_delegate imageForIndex:nextNo];

        

        if ([self.delegaterespondsToSelector:@selector(imageURLForIndex:)]) {

            [_leftViewsd_setImageWithURL:[_delegateimageURLForIndex:number-1]placeholderImage:[UIImagenew]completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,NSURL *imageURL) {

                //            NSLog(@"");

            }];

            [_middleViewsd_setImageWithURL:[_delegateimageURLForIndex:_currentPageNo]placeholderImage:[UIImagenew]completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,NSURL *imageURL) {

                

            }];

            [_rightViewsd_setImageWithURL:[_delegateimageURLForIndex:nextNo]placeholderImage:[UIImagenew]completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,NSURL *imageURL) {

                

            }];

        } elseif ([self.delegaterespondsToSelector:@selector(imageForIndex:)]){

            _leftView.image = [_delegateimageForIndex:number-1];

            _middleView.image = [_delegateimageForIndex:_currentPageNo];

            _rightView.image = [_delegateimageForIndex:nextNo];

        }

    }

    

    if ([_delegaterespondsToSelector:@selector(didSelectAtIndex:)]) {

        _middleView.userInteractionEnabled =YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(selectAction:)];

        [_middleViewaddGestureRecognizer:tap];

    }

}

 

-(void)selectAction:(UITapGestureRecognizer *)gesture {

    if ([self.delegaterespondsToSelector:@selector(didSelectAtIndex:)]) {

        [self.delegatedidSelectAtIndex:_currentPageNo];

    }

}

 

-(void)autoLoop{

    _currentPageNo++;

    //自动循环

    [_scrollViewscrollRectToVisible:CGRectMake(_scrollView.bounds.size.width*2,0,_scrollView.bounds.size.width,_scrollView.bounds.size.height)animated:YES];

}

 

  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    

//    NSLog(@"scrollViewDidEndDecelerating");

    int pageNo = scrollView.contentOffset.x/scrollView.bounds.size.width;

    long imgCount = [_delegatenumberOfImagesInScrollView];

//    NSLog(@"pageNo : %i",pageNo);

    if (pageNo ==0) {

        _currentPageNo--;

    } elseif (pageNo ==2){

        _currentPageNo++;

    }

    

    if (_currentPageNo <0) {

        _currentPageNo = (int)(imgCount -1);

    } elseif (_currentPageNo > imgCount -1) {

        _currentPageNo =0;

    }

    

    int previousPage =_currentPageNo -1;

    if (previousPage <0) {

        previousPage = (int)(imgCount -1);

    }

    

    int nextPage =_currentPageNo +1;

    if (nextPage > imgCount-1) {

        nextPage = 0;

    }

    _pageControl.currentPage =_currentPageNo;

    

    if ([self.delegaterespondsToSelector:@selector(imageURLForIndex:)]) {

        [_leftViewsd_setImageWithURL:[_delegateimageURLForIndex:previousPage]placeholderImage:[UIImagenew]completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,NSURL *imageURL) {

        }];

        [_middleViewsd_setImageWithURL:[_delegateimageURLForIndex:_currentPageNo]placeholderImage:[UIImagenew]completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,NSURL *imageURL) {

            

        }];

        [_rightViewsd_setImageWithURL:[_delegateimageURLForIndex:nextPage]placeholderImage:[UIImagenew]completed:^(UIImage *image,NSError *error,SDImageCacheType cacheType,NSURL *imageURL) {

            

        }];

    } elseif ([self.delegaterespondsToSelector:@selector(imageForIndex:)]){

        _leftView.image = [_delegateimageForIndex:previousPage];

        _middleView.image = [_delegateimageForIndex:_currentPageNo];

        _rightView.image = [_delegateimageForIndex:nextPage];

    }

   

    _scrollView.contentOffset =CGPointMake(scrollView.bounds.size.width,0);

}

 

@end

在iOS开发中,经常会在APP首页看到多张图片进行轮换。刚开始做的时候,感觉很麻烦,不是很好做,查...

/** *  照片浏览的下标 1/8 * */
#import <UIKit/UIKit.h>

图片 1

////  ImageVC.h//  MovieCrowdfunding////  Created by Sun-Mac on 2016/11/1.//  Copyright © 2016年  All rights reserved.//#import@interface ImageVC : UIViewController

轮播图播放的主要技术在于:

@interface PageLabel : UILabel

// 最下面的scrollView

@property(strong,nonatomic)NSURL *imageURL;//创建vc对象之后要传imageURL

cell的封装。这里采用UICollectionViewCell实现。

@property (nonatomic,assign)NSInteger numberOfPages;
@property (nonatomic,assign)NSInteger currentPage;

@property (nonatomic, strong) UIScrollView *scrollView;

@end

#import <UIKit/UIKit.h>

/** *  page改变 */
- (void)labelText;

// 网址数组

//

@interface CircleViewCell : UICollectionViewCell

@end

@property (nonatomic, strong) NSArray *imageURLs;

//  ImageVC.m

@property (nonatomic, strong) UIImage* image;

 

@property (nonatomic, strong) UIPageControl *pageController;

//  MovieCrowdfunding

@property (nonatomic, assign) NSInteger index;

#import "PageLabel.h"

// 定时器 当用户拖拽时 不需要自动滑动 所以设置为属性

//

@end

@implementation PageLabel

@property (nonatomic, strong) NSTimer *timer;

//  Created by Sun-Mac on 2016/11/1.

//

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.textColor = [UIColor whiteColor];
        self.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

@end

//  Copyright © 2016年 . All rights reserved.

//  CircleViewCell.m

- (void)labelText {
    if (_numberOfPages > 0) {
        self.text = [NSString stringWithFormat:@"%ld/%ld",_currentPage+1,_numberOfPages];
    }else {
        self.text = nil;
    }
}

@implementation CarouselView

//

//  lunbo

@end

// 传入frame和装有网址的数组

#import "ImageVC.h"

//

 也可以用UIPageControl也差不多

- (instancetype)initWithFrame:(CGRect)frame imageURLs:(NSArray *)imageURLs {

@interface ImageVC ()

//  Created by  JIAOXIANGJIE on 16/10/12.

图片浏览控制器

self = [super initWithFrame:frame];

{

//  Copyright © 2016年 lumen. All rights reserved.

 CycleScrollViewController.h

if (self) {

UIScrollView *scrollView;

//

 

if (imageURLs.count == 0) {

UIImageView *imageView;

 

#import <UIKit/UIKit.h>
@protocol CycleScrollViewDelegate <NSObject>

return nil;

}

#import "CircleViewCell.h"

- (void)updateui;

}

@end

 

/**
 *  删除
 *
 *  @param array 返回改变后的数组
 */
- (void)cycleScrollViewDeletItem:(NSArray *)array;

self.imageURLs = imageURLs;

@implementation ImageVC

@interface CircleViewCell()

@end

[self createScrollView];

- (void)viewDidLoad

 

@interface CycleScrollViewController : UIViewController

[self initImages];

{

@property (nonatomic, strong) UIImageView* imageview;

@property (nonatomic,assign)id<CycleScrollViewDelegate>delegate;

[self createPageController];

[super viewDidLoad];

 

/**
 *  初始化
 *
 *  @param mixId  数组(名字或者image)
 *  @param index  下标
 *  @param myself 是不是自己(如果是自己久传入image不是的话就传入网络地址)
 *
 *  @return 
 */

// 两张图以上才创建timer

self.view.backgroundColor = [UIColor blackColor];

@end

- (instancetype)initWithMixids:(NSArray *)mixId currentIndex:(int)index myself:(BOOL)myself;

if (self.imageURLs.count >= 2) {

scrollView=[[UIScrollView alloc]initWithFrame:self.view.bounds];

 

@end

[self createTimer];

scrollView.maximumZoomScale=5.0;//图片的放大倍数

@implementation CircleViewCell

 

}

scrollView.minimumZoomScale=1.0;//图片的最小倍率

 

 CycleScrollViewController.m

}

scrollView.contentSize=CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);

-(instancetype)init{

//
//  ViewController.m
//  test
//
//  Created by 张鹏 on 14-4-30.
//  Copyright (c) 2014年 rimi. All rights reserved.
//

return self;

scrollView.delegate=self;

    if(self = [super init]){

#import "CycleScrollViewController.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "PageLabel.h"
@interface CycleScrollViewController () <UIScrollViewDelegate,UIAlertViewDelegate> {
    
    NSMutableArray *_imageNames;
    NSMutableArray *_imageViews;
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
    NSInteger _currentPageIndex;
    PageLabel *_labelText;
    
}

}

imageView=[[UIImageView alloc]initWithFrame:self.view.bounds];

        [self myInit];

@property (nonatomic,strong) UITapGestureRecognizer *singleTap;

- (void)createPageController {

[imageView setImageWithURL:self.imageURL placeholderImage:[UIImage imageNamed:@"1"]];

    }

@property (nonatomic,strong) UITapGestureRecognizer *doubleTap;

self.pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 60, self.frame.size.height - 20, 50, 20)];

[scrollView addSubview:imageView];

    

@property (nonatomic,assign) CGFloat zoomScale;

[self addSubview:self.pageController];

[self.view addSubview:scrollView];

    return self;

@property (nonatomic,strong) UIDynamicAnimator *animator;

// 一共几个diandian

imageView.userInteractionEnabled=YES;//注意:imageView默认是不可以交互,在这里设置为可以交互

}

@property (nonatomic) BOOL myself;

self.pageController.numberOfPages = self.imageURLs.count;

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapImage:)];

 

@property (nonatomic) BOOL deleted;

}

tap.numberOfTapsRequired=1;//单击

-(instancetype)initWithCoder:(NSCoder *)aDecoder

- (void)initializeUserInterface;

- (void)createScrollView {

tap.numberOfTouchesRequired=1;//单点触碰

{

//- (void)updateScrollViewWithContentOffset:(CGPoint)contentOffset;

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

[imageView addGestureRecognizer:tap];

    if(self = [super initWithCoder:aDecoder])

//- (NSInteger)actualIndexWithIndex:(NSInteger)index;

self.scrollView.contentSize = CGSizeMake(self.frame.size.width * self.imageURLs.count, 0);

UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];

        [self myInit];

@end

self.scrollView.delegate = self;

doubleTap.numberOfTapsRequired=2;//避免单击与双击冲突

    

@implementation CycleScrollViewController

self.scrollView.backgroundColor = [UIColor yellowColor];

[tap requireGestureRecognizerToFail:doubleTap];

    return self;

- (instancetype)initWithMixids:(NSArray *)mixId currentIndex:(int)index myself:(BOOL)myself{
    
    self = [super init];
    if (self) {
        _myself=myself;
        _deleted=NO;
        _imageNames = [NSMutableArray arrayWithArray:mixId];
        _imageViews = [NSMutableArray array];
        _currentPageIndex = index;
    }
    return self;
}

[self addSubview:self.scrollView];

[imageView addGestureRecognizer:doubleTap];

}

- (void)dealloc {
    
    _imageNames = nil;
    _imageViews = nil;
}

}

imageView.contentMode=UIViewContentModeScaleAspectFit;

 

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initializeUserInterface];
}

- (void)initImages{

}

-(instancetype)initWithFrame:(CGRect)frame

- (void)initializeUserInterface {
    
    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectInset(self.view.bounds, -4, 0)];
    _scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.bounds) * _imageNames.count,
                                         CGRectGetHeight(_scrollView.bounds));
    _scrollView.contentOffset = CGPointMake(CGRectGetWidth(_scrollView.bounds)*(_currentPageIndex), 0);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bouncesZoom = YES;
    _scrollView.bounces = YES;
    _scrollView.canCancelContentTouches = YES;
    _scrollView.delaysContentTouches = YES;
    
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    if (_imageNames.count == 1) {
        _scrollView.scrollEnabled = NO;
    }
    
    [self.view addSubview:_scrollView];
    
    for (int i = 0; i < _imageNames.count; i++) {
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.bounds = self.view.frame;
        scrollView.center = CGPointMake(CGRectGetWidth(_scrollView.bounds) / 2 + CGRectGetWidth(_scrollView.bounds) * i,
                                        CGRectGetHeight(_scrollView.bounds) / 2);
        scrollView.contentSize = self.view.frame.size;
        scrollView.delegate = self;
        scrollView.maximumZoomScale = 2.0;
        scrollView.minimumZoomScale = 1.0;
        scrollView.zoomScale = 1.0;
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        [_scrollView addSubview:scrollView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.layer.masksToBounds = YES;
        imageView.userInteractionEnabled = YES;
        [_imageViews addObject:imageView];
        if (_imageNames) {
            if (_myself) {
                imageView.image = _imageNames[i];
            }else {
                [imageView sd_setImageWithURL:[NSURL URLWithString:_imageNames[i]]
                             placeholderImage:[UIImage imageNamed:@"four_contentimage"]
                                      options:SDWebImageCacheMemoryOnly
                                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                    }];
            }
           
        }
        
        [scrollView addSubview:imageView];
        
        
            self.singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
            self.singleTap.numberOfTapsRequired = 1;
            [imageView addGestureRecognizer:self.singleTap];
            
            self.doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
            self.doubleTap.numberOfTapsRequired = 2;
            [imageView addGestureRecognizer:self.doubleTap];
            
            [self.singleTap requireGestureRecognizerToFail:self.doubleTap];
    }
    _labelText = [[PageLabel alloc]init];
    _labelText.bounds = CGRectMake(0, 0, 320, 20);
    _labelText.center = CGPointMake(CGRectGetMidX(self.view.bounds),
                                      CGRectGetMaxY(self.view.bounds) - 50);
    if (_imageNames.count >1) {
        _labelText.numberOfPages = [_imageNames count];
    }
    else {
        _labelText.numberOfPages = 0;
    }
    [self.view addSubview:_labelText];
    _labelText.currentPage = _currentPageIndex;
    [_labelText labelText];
    if (_myself) {
        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-20)/2, 40, 20, 20)];
        img.image=[UIImage imageNamed:@"ic_delete"];
        UIButton *delbtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 30)];
        delbtn.backgroundColor=[UIColor clearColor];
        [delbtn addTarget:self action:@selector(ButtonPressed) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:img];
        [self.view addSubview:delbtn];
    }
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

// 如果只有一个网址(一张图片)不用循环轮播 直接创建一张imageView

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView  //委托方法,必须设置  delegate

{

}

if (self.imageURLs.count == 1) {

{

    if(self = [super initWithFrame:frame])

- (void)popLastViewController {
    
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

return imageView;//要放大的视图

        [self myInit];

- (void)ButtonPressed{
    UIAlertView *view=[[UIAlertView alloc] initWithTitle:nil message:@"确定要删除该照片?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [view show];
}

[imageView sd_setImageWithURL:[NSURL URLWithString:self.imageURLs[0]]];

}

    

#pragma mark - tapGestureEvent

[self.scrollView addSubview:imageView];

-(void)doubleTap:(id)sender

    return self;

- (void)singleTap:(UITapGestureRecognizer *)tap {
    if (_deleted) {
        [_delegate updateui];
    }
    [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFunction)];

{

}

- (void)doubleTap:(UITapGestureRecognizer *)tap {
    
    UIScrollView *scrollView = (UIScrollView *)tap.view.superview;
    NSLog(@"doubleTap");
    
//    if (_isDetailShown) {
//        return;
//    }
    
    if (scrollView.zoomScale == scrollView.minimumZoomScale) {
        // Zoom in
        CGPoint center = [tap locationInView:scrollView];
        CGSize size = CGSizeMake(scrollView.bounds.size.width / scrollView.maximumZoomScale,
                                 scrollView.bounds.size.height / scrollView.maximumZoomScale);
        CGRect rect = CGRectMake(center.x - (size.width / 2.0), center.y - (size.height / 2.0), size.width, size.height);
        [scrollView zoomToRect:rect animated:YES];
    }
    else {
        // Zoom out
        [scrollView zoomToRect:scrollView.bounds animated:YES];
    }
}

// 给每一张图片都添加一个点击事件

scrollView.zoomScale=2.0;//双击放大到两倍

 

#pragma mark - UIScrollViewDelegate methods

[imageView addGestureRecognizer:tap];

}

-(void)layoutSubviews

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    for (UIView *v in scrollView.subviews){
        return v;
    }
    return nil;
}

// 打开imageView交互

- (IBAction)tapImage:(id)sender

{

#pragma mark - <UIScrollViewDelegate>

imageView.userInteractionEnabled = YES;

{

    [super layoutSubviews];

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int index= scrollView.contentOffset.x/self.view.bounds.size.width;
    _currentPageIndex=index;
    [_labelText setCurrentPage:index];
    [_labelText labelText];
}

return;

[self dismissViewControllerAnimated:YES completion:nil];//单击图像,dismissViewController

    

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
        NSLog(@"%ld",_currentPageIndex);
        for (int i = 0; i < _imageNames.count; i ++) {
            if (i == _currentPageIndex) {
                [_imageNames removeObjectAtIndex:i];
            }
        }
        if ([_delegate respondsToSelector:@selector(cycleScrollViewDeletItem:)]) {
            [_delegate cycleScrollViewDeletItem:_imageNames];
        }
    }
}

}

}

    self.imageview.frame = self.contentView.bounds;

@end

// 在左右两边各添加一张imageView

@end

}

 

self.scrollView.contentSize = CGSizeMake(self.frame.size.width * (self.imageURLs.count + 2), 0);

 

// 整 页滚动

-(UIImageView*)imageview

self.scrollView.pagingEnabled = YES;

{

// 一上来就显示第一个

    if(_imageview == nil){

self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);

        _imageview = [[UIImageView alloc] init];

for (int i = 0; i < self.imageURLs.count + 2; i++) {

        

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClickFunction)];

        _imageview.contentMode = UIViewContentModeScaleAspectFill;

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];

        _imageview.userInteractionEnabled = YES;

// 给每一张图片都添加一个点击事件

        _imageview.backgroundColor = [UIColor whiteColor];

[imageView addGestureRecognizer:tap];

    }

// 打开imageView交互

    

imageView.userInteractionEnabled = YES;

    return _imageview;

// 第一张显示最后一张的内容

}

本文由美高梅赌堵59599发布于联系我们,转载请注明出处:// 传入frame和装有网址的数组,  滚动图片总数

关键词: