新浪微博项目之新特性界面

实现分析

  • 控制器的 view上有一个 scrollview,scrollview 上添加 view;
  • UIPageController添加到控制器 view 上(不能加到scrollview)

代码实现

  • 新特性页面GGNewfeatureViewController
#define GGNewfeatureCount 4
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 1.创建一个scrollView:显示所有的新特性图片

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;

    // 2.添加图片到scrollView中
    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    for (int i = 0; i < GGNewfeatureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        // 显示图片
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];


        // 如果是最后一个imageView,就往里面添加其他内容
        if (i == GGNewfeatureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
#warning 默认情况下,scrollView一创建出来,它里面可能就存在一些子控件了
#warning 就算不主动添加子控件到scrollView中,scrollView内部还是可能会有一些子控件
    // 3.设置scrollView的其他属性
    // 如果想要某个方向上不能滚动,那么这个方向对应的尺寸数值传0即可
    scrollView.contentSize = CGSizeMake(GGNewfeatureCount * scrollW, 0);
    scrollView.bounces = NO; // 去除弹簧效果
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;

    // 4.添加pageControl:分页,展示目前看的是第几页
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = GGNewfeatureCount;
    pageControl.currentPageIndicatorTintColor = GGColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = GGColor(1889, 189, 189);
    pageControl.centerX = scrollW * 0.5;
    pageControl.centerY = scrollH - 50;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}
  • 计算页面方法 (int)(page + 0.5)
    • 1.3四舍五入 1.3 + 0.5 = 1.8 强转为整数(int)1.8= 1
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    double page = scrollView.contentOffset.x / scrollView.width;
    // 四舍五入计算出页码
    self.pageControl.currentPage = (int)(page + 0.5);
}
  • 实现最后一个 view
/**
 *  初始化最后一个imageView
 */
- (void)setupLastImageView:(UIImageView *)imageView
{
    // 开启交互功能
    imageView.userInteractionEnabled = YES;

    // 1.分享给大家(checkbox)
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    shareBtn.width = 200;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.centerY = imageView.height * 0.65;
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareBtn];

    // 2.开始微博
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = shareBtn.centerX;
    startBtn.centerY = imageView.height * 0.75;
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}
  • titleEdgeInsets、imageEdgeInsets和contentEdgeInsets对比
    • EdgeInsets: 自切
      • contentEdgeInsets:会影响按钮内部的所有内容(里面的imageView和titleLabel)
      • titleEdgeInsets:只影响按钮内部的titleLabel
      • imageEdgeInsets:只影响按钮内部的imageView
  • 取反逻辑
- (void)shareClick:(UIButton *)shareBtn
{
    // 状态取反
    shareBtn.selected = !shareBtn.isSelected;
}
  • 新特性界面完,跳转首页
- (void)startClick
{
 // 切换window的rootViewController
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[HWTabBarViewController alloc] init];
}

版本判断

这里写图片描述

  • AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 1.创建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;

    // 2.设置根控制器
    NSString *key = @"CFBundleVersion";
    // 上一次的使用版本(存储在沙盒中的版本号)
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    // 当前软件的版本号(从Info.plist中获得)
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
//    self.window.rootViewController = [[GGTabBarController alloc] init];
    if ([currentVersion isEqualToString:lastVersion]) { // 版本号相同:这次打开和上次打开的是同一个版本
        self.window.rootViewController = [[GGTabBarController alloc] init];
    } else { // 这次打开的版本和上一次不一样,显示新特性
        self.window.rootViewController = [[GGNewfeatureViewController alloc] init];

        // 将当前的版本号存进沙盒
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

    // 4.显示窗口
    [self.window makeKeyAndVisible];
    return YES;
}

更换系统自带 TabBar 注意事项

 GGTabBar *tabBar = [[HWTabBar alloc] init];
 tabBar.delegate =self;  // 注意顺序,不能颠倒
 [self setValue:tabBar forKeyPath:@"tabBar"];
  • [self setValue:tabBar forKeyPath:@"tabBar"];这行代码过后,tabBar的delegate就是
    GGTabBarViewController
    • 不用再设置tabBar.delegate = self;
  • 注意:
    • 如果tabBar设置完delegate后,再执行tabBar.delegate = self;修改delegate,就会报错
 错误信息:Changing the delegate of a tab bar managed by a tab bar controller is not allowed.
 错误意思:不允许修改TabBar的delegate属性(这个TabBar是被TabBarViewController所管理的)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值