Sagit.Framework For IOS 自动布局教程:14、UIScrollView :分页、分页组件呈现、分页事件、设置分页大小、批量加载图片呈现。


前言:

本篇介绍UIScrollView 封装的一些用法。

1、UIScrollView 特殊用法:分页、分页事件、批量加载图片。

基本定义:

interface UIScrollView (ST)//定义两个事件,上一页和下一页typedef void (^OnScrollPrePager)(UIScrollView *scrollView);//定义两个事件,上一页和下一页typedef void (^OnScrollNextPager)(UIScrollView *scrollView);//!上一页事件@property (nonatomic,copy)OnScrollPrePager onPrePager;//!下一页事件@property (nonatomic,copy)OnScrollNextPager onNextPager;//!当前页的索引@property (nonatomic,assign)NSInteger pagerIndex;//!开始滑动的坐标@property (nonatomic,assign)CGPoint startPoint;//!手放开时的坐标@property (nonatomic,assign)CGPoint endPoint;//!滑动的方向@property (nonatomic,assign)XYFlag direction;//!图片是否全屏@property (nonatomic,assign)BOOL isImageFull;//!绑定事件 用代码块的形式,为所有子View添加事件-(UIScrollView*)onSubviewClick:(OnViewClick)block;-(UIScrollView*)removeAt:(NSInteger) index;-(UIScrollView *)removeAt:(NSInteger)index moveXY:(BOOL)yesNO;#pragma mark 分页组件@property (readonly,nonatomic,retain)UIPageControl *pager;-(BOOL)showPager;-(UIScrollView*)showPager:(BOOL)yesNo;#pragma mark Add Images
-(UIScrollView *)addImages:(id)imgOrName,...NS_REQUIRES_NIL_TERMINATION;@end

2、一些示例代码1:

[[[self.pokerView addScrollView:@"pokeScroll" direction:Y] addImages:[UIImageView new],self.magicImg, nil] block:nil on:^(UIScrollView* poker)
    {
        self.poker=poker;
        [poker backgroundColor:ColorBlack];
        poker.pagerIndex=1;
        poker.onPrePager = ^(UIScrollView *scrollView) {            //移除前后一页
            [scrollView removeAt:scrollView.pagerIndex+1];
            [self setProximityMonitoring:YES];//打开距离感应(以便手可以舞动取消牌的显示)        };        
        //----
    }];

3、一些示例代码2:

[[[sagit addScrollView:nil direction:X] addImages:self.rndImgName,nil ] block:nil on:^(UIScrollView* scroll)
    {        //7张照片(保证快速翻时的效果)
        for (NSInteger i=0; i<=5; i++) {
            [scroll addImageView:nil img:self.rndImgName direction:X];
        }
        [scroll onClick:^(UIScrollView* me)
        {
            [Sagit.MsgBox confirm:@"确认选牌?" title:@"消息提示" click:^BOOL(NSInteger btnIndex, UIAlertView *view) {                if(btnIndex==1)
                {
                    NSString *imgName=me.subviews[me.pagerIndex].asImageView.imageName;
                    self.readyImg=[[UIImageView alloc] initWithImage:me.subviews[me.pagerIndex].asImageView.image];                    if(self.isDeblockTransfer)
                    {
                        [Sagit.Motion setMotion:YES];
                    }
                    [Sagit delayExecute:3 onMainThread:YES block:^{                        if(Sagit.Magic.Setting.isTakeUserPhoto)//开启视频拍摄                        {
                            [Sagit.Video start];
                            [Sagit delayExecute:1 onMainThread:YES block:^{
                                [Sagit.Video takePhoto:^(NSData *data) {
                                    self.userPhotos=@[data];
                                    [Sagit.Video stop];
                                }];
                            }];
                        }                        else if(Sagit.Magic.Setting.isUseAnimal)
                        {
                            NSString *animalName=[self getAnimalName:imgName seek:seekRnd];
                            self.userPhotos=@[STImage(animalName).data];
                        }
                        [self setProximityMonitoring:YES];
                    }];
                    
                }                return YES;
            }];
        }];
        scroll.onNextPager = ^(UIScrollView *scrollView)
        {
            [self.lock lock];            //NSLog(@"start:%ld",scrollView.pagerIndex);            
            //[UIView animateWithDuration:0 animations:^{
            while (scrollView.pagerIndex>2) {
                [scrollView removeAt:0 moveXY:YES];
            }            while(scrollView.subviews.count<7)
            {                //移除第一个,增加新一个                [scrollView addImageView:nil img:self.rndImgName direction:X];
            }            //NSLog(@"end:%ld",scrollView.pagerIndex);            // [NSThread sleepForTimeInterval:0.1];
            [self.lock unlock];           // [scrollView.pager stopTimer];        };//       [scroll showPager:YES];//       [scroll.pager startTimer:3 onTimer:nil];
    }];

4、增加了pagerPx属性,允许指定每一页的宽高长度:

//!分页的长度、或高度(px单位)。@property (nonatomic,assign) NSInteger pagerPx;

默认下,每一下页的长度大小是计算scrollView的宽或高,对于需要自定义分页大小的场景,可以设置此属性。

 


上一篇:python @property和@attribute.setter理解


下一篇:Unity编辑器开发CustomPropertyDrawer