IOS从零开发一款App(三)(UIScrollview实现ViewPager 效果)

1. UIScrollView 实现 ViewPager 的效果(横向滑动切换页面)

新建RecommondViewController.m

#import "RecommendViewController.h"

@interface RecommendViewController ()

@end

@implementation RecommendViewController

- (instancetype)init{
    if(self = [super init]){
    self.view.backgroundColor = [UIColor blueColor];
    self.tabBarItem.title = @"推荐";
    self.tabBarItem.image = [UIImage imageNamed: @"icon.bundle/like@2x.png"];
    self.tabBarItem.selectedImage = [UIImage imageNamed: @"icon.bundle/like_selected@2x.png"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:self.view.bounds];
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width*5, self.view.bounds.size.height);
    NSArray *colors = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor],[UIColor blackColor],[UIColor greenColor]];
    for(int i=0;i<5;i++){
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        view.backgroundColor = colors[i];
        [scrollView addSubview:view];
    }
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    [self.view addSubview:scrollView];
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rQYGzkOn-1619711963604)(https://tva1.sinaimg.cn/large/008i3skNgy1gq0ligs4xng30k016gnq5.gif)]

2.自定义UITabViewCell

  • 1.新建一个TestTableViewCell,重写它的构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self.contentView addSubview:({
            self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 15,  300, 50)];
            self.titleLabel.font =[UIFont systemFontOfSize:16];
            self.titleLabel.textColor =[UIColor blackColor];
            self.titleLabel.backgroundColor =[UIColor redColor];
            self.titleLabel;
        })];
        [self.contentView addSubview:({
            self.sourceLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 80,  50, 20)];
            self.sourceLabel.backgroundColor =[UIColor redColor];
            self.sourceLabel.font =[UIFont systemFontOfSize:12];
            self.sourceLabel.textColor =[UIColor blackColor];
            self.sourceLabel;
        })];
        [self.contentView addSubview:({
            self.commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 80, 50, 20)];
            self.commentLabel.backgroundColor =[UIColor redColor];
            self.commentLabel.font =[UIFont systemFontOfSize:12];
            self.commentLabel.textColor =[UIColor blackColor];
            self.commentLabel;
        })];
        [self.contentView addSubview:({
            self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 80,  50, 20)];
            self.timeLabel.backgroundColor =[UIColor redColor];
            self.timeLabel.font =[UIFont systemFontOfSize:12];
            self.timeLabel.textColor =[UIColor blackColor];
            self.timeLabel;
        })];
    }
    return self;
    
}
  • UILabel 的自适应
-(void)layoutTableViewCell{
    self.titleLabel.text = @"我是海贼王";
    self.sourceLabel.text = @"路飞";
    [self.sourceLabel sizeToFit];
    self.commentLabel.text = @"娜美";
    self.commentLabel.frame = CGRectMake(self.sourceLabel.frame.origin.x+self.sourceLabel.frame.size.width+15, self.sourceLabel.frame.origin.y, self.commentLabel.frame.size.width, self.commentLabel.frame.size.height);
    [self.commentLabel sizeToFit];
    self.timeLabel.text = @"山治";
    self.timeLabel.frame = CGRectMake(self.commentLabel.frame.origin.x+self.commentLabel.frame.size.width+15, self.commentLabel.frame.origin.y, self.timeLabel.frame.size.width, self.timeLabel.frame.size.height);
    [self.timeLabel sizeToFit];
}

屏幕录制2021-04-29 上午12.08.49

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
一个类似于安卓ViewPager的开源库 - iOS ViewPager 高级库 支持 iPhone/ipad/ipod 相关方法 pragma mark - version 1.0 /** * 初始化 YFViewPager的方法 * * @param frame frame * @param titles 标题数组 * @param views 视图数组 和标题数组一一对应 * * @return YFViewPager */ - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles views:(NSArray *)views; /** * 设置选择的菜单按钮 * * @param index 按钮的索引值 从左到右一次是0,1,2,3... */ - (void)setSelectIndex:(NSInteger)index; /** * 点击菜单按钮时 调用的block方法 * * @param block 返回YFViewPager本身和点击的按钮的索引值,从左到右一次是0,1,2,3... */ - (void)didSelectedBlock:(SelectedBlock)block; pragma mark - version 2.0 /** * 初始化 YFViewPager的方法 也是目前使用的YFViewPager的唯一初始化api * * @param frame frame * @param titles 标题数组 * @param icons 标题右侧图标数组 * @param selectedIcons 标题右侧选中时的图标数组 * @param views 视图数组 和标题数组一一对应 * * @return YFViewPager */ - (id)initWithFrame:(CGRect)frame titles:(NSArray *)titles icons:(NSArray *)icons selectedIcons:(NSArray *)selectedIcons views:(NSArray *)views; /** * 设置菜单标题左边的icon 图标 * * @param icons 图标image * @param selectedIcons 菜单被选中时显示的图标image */ - (void)setTitleIconsArray:(NSArray *)icons selectedIconsArray:(NSArray *)selectedIcons; /** * 设置菜单右上角小红点显示的文字,数组需与菜单一一对应,数字为0时 赋值 @0或@"" * * @param tips 小红点上的文字 */ - (void)setTipsCountArray:(NSArray *)tips;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小菜的OnePiece

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值