轮播图不同实现方法 以及 发布到Cocoapod

图片发自简书App

Demo地址
GitHub

周末闲着无聊把使用两个UIImageView 以及使用一个UIImageView 来实现轮播图分别写了一下,顺便试了下发布到Cocoapod,总体来说还是很简单的,记录下流程。

两个UIImageView

原理和使用三个基本一样,没什么可以说的
使用的demo

- (void)demo{
    JCCarouselView *bannerView = [[JCCarouselView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 220)];
    [self.view addSubview:bannerView];
    bannerView.imageUrlArr = self.imageUrlArr;
    
    bannerView.placeholderImage = [UIImage imageNamed:@"placeholder"];
    
    //设置pagecontrol 图片
    bannerView.curPageControlImage = [UIImage imageNamed:@"Group"];
    bannerView.pageControlImage = [UIImage imageNamed:@"Group1"];
    
    //设置pagecontrol 颜色
    bannerView.pageControlColor = [UIColor whiteColor];
    bannerView.curPageControlColor = [UIColor redColor];
    
    bannerView.timeInterval = 4;
    bannerView.delegate = self;
    
    bannerView.clickBlock = ^(NSInteger index){
        NSLog(@"点击%ld张图片",index);
    };
}

- (void)carouselView:(JCCarouselView *)carouselView didSelectedAtIndex:(NSInteger)index{
    NSLog(@"点击%ld张图片",index);
}

一个UIImageView

主要原理是使用CATransition 这个类,往图片上添加转场效果。

先在图片上添加左滑以及右滑的手势

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:_imageView];
        [_imageView setContentMode:UIViewContentModeScaleAspectFill];
        _imageView.clipsToBounds = YES;
        _imageView.userInteractionEnabled = YES;
        
        UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftSwipe:)];
        leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
        [_imageView addGestureRecognizer:leftSwipeGesture];
        
        UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightSwipe:)];
        rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [_imageView addGestureRecognizer:rightSwipeGesture];
    }
    return _imageView;
}

滑动的实现方法

-(void)transitionAnimation:(NSInteger)direction{
    CATransition *transition=[[CATransition alloc]init];
    transition.type= kCATransitionPush;//@"rippleEffect";
    
    BOOL right = YES;
    
    //设置子类型
    if (direction == JCCarouselDirectionLeft) {
        transition.subtype=kCATransitionFromRight;
        
    }else if (direction == JCCarouselDirectionRight) {
        transition.subtype=kCATransitionFromLeft;
        right = NO;
    }
    
    transition.duration = .25f;
    
    if (right) {
        self.curIndex=(_curIndex + 1) % self.imageUrlArr.count;
    }else{
        self.curIndex=(_curIndex - 1 + self.imageUrlArr.count) % self.imageUrlArr.count;
    }
    [self.imageView sd_setImageWithURL:self.imageUrlArr[self.curIndex]];
    [self.imageView.layer addAnimation:transition forKey:@"KCTransitionAnimation"];
}

这种方法的局限在于不能跟随手势滑动,只要滑动就会立刻切换图片,实用性不大,不过了解下CATransition 这个类还是很有好处的。

发布到Cocoapod

1.打tag

git tag -a 1.0.0 -m"1.0.0"
git push --tags

2.创建项目的podspec文件

pod spec create JCCarouselView

这时会生成JCCarouselView.podspec 文件,这就相当于你的库的描述文件,语法参照这里填写即可

Pod::Spec.new do |s|

  s.name         = "JCCarouselView"
  s.version      = "1.0.4"
  s.summary      = "两个UIImageView实现的轮播banner"

  s.homepage     = "https://github.com/JiachengZheng/JCCarouselView"

  s.license      = { :type => "MIT", :file => "FILE_LICENSE" }
  s.author       = { "zhengjiacheng" => "jiachengzheng@163.com" }
  s.platform     = :ios, "7.0"
  s.ios.deployment_target = "7.0"
  s.source       = { :git => 'https://github.com/JiachengZheng/JCCarouselView.git', :tag => s.version }
  s.source_files  = 'JCCarouselView/*.{h,m}'

  # s.resource  = "icon.png"
  # s.resources = "Resources/*.png"
  s.public_header_files = 'JCCarouselView/*.h'
  s.requires_arc = true

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  s.dependency 'SDWebImage'

end

3.验证podspec文件

pod spec lint JCCarouselView.podspec

不知道为什么总会提示报错

Updating spec repo `master`
Validating podspec
 -> JCCarouselView (1.0.2)
    - ERROR | [iOS] xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
    - ERROR | xcodebuild:  /Users/zhengjiacheng/Library/Developer/Xcode/DerivedData/App-dvilxxenwfwalycxbchznfaznvzq/Build/Products/Release-iphonesimulator/JCCarouselView/JCCarouselView.framework/Headers/JCCarouselView.h:10:9: error: include of non-modular header inside framework module 'JCCarouselView.JCCarouselView' [-Werror,-Wnon-modular-include-in-framework-module]
    - NOTE  | xcodebuild:  /var/folders/8j/zd9r7fv92wz32qbj6dhk70n80000gn/T/CocoaPods/Lint/App/main.m:3:9: fatal error: could not build module 'JCCarouselView'

[!] The spec did not pass validation, due to 2 errors.
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run: 
    `echo "2.3" > .swift-version`.

后来改为

pod spec lint JCCarouselView.podspec --use-libraries

就可以了,不太知道原因。。。

如果出现

 -> JCCarouselView (1.0.2)

Analyzed 1 podspec.

JCCarouselView.podspec passed validation.

就代表成功了

4.上传podspec

首先需要注册

pod trunk register xxxxxx@gmail.com 'MrSong' --description='MrSong'

随后邮箱里会发确认邮件,确认以后就可上传了
可通过 pod trunk me 查看自己是否注册成功

之后就可以上传了,这一步可能会比较慢

pod trunk push JCCarouselView.podspec --use-libraries

后面还是加上了--use-libraries防止报错。。。

出现如下代码上传成功

   Congrats

   JCCarouselView (1.0.2) successfully published
   February 6th, 02:16
   https://cocoapods.org/pods/JCCarouselView
   Tell your friends!

5.验证

pod search 'JCCarouselView'

大功告成!Enjoy!

Demo地址
GitHub

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容