[Apple TV 开发教程] 之一 你好,Apple TV!

本文属于NilStack 写作的《Apple TV app 开发教程》的第一篇。翻译讲究信达雅,因为时间和水平有限,部分地方在自己理解的基础上翻译的。更多文章请访问本人在GitHub 的博客


苹果公司发布tvOS 和新一代Apple TV 后,Apple TV 成了另一个开发平台。


程序员要做的第一件事,自然是开发一个“Hello Apple TV!”程序。


先从苹果开发者网站上下载Xcode 7.1 beta 并安装。启动Xcode 后,在选择模板的界面,可以看到增加了一个tvOS 分类,选择“Single View Application”。tvOS-new-project-template


接下来的步骤和创建iOS 应用是一模一样的,生成的文件你应该也很熟悉。

tvOS-new-project-files


我们在ViewController 的实现文件里增加UI 元素和业务逻辑。我们简单的用一个Label 显示“Hello TV!”,并且在Apple TV 遥控器里点击“播放/暂停”按钮后将改变文字的颜色。

//
//  ViewController.m
//  HelloTV
//
//  Created by Peng on 9/10/15.
//  Copyright © 2015 Peng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic) UILabel *label;
@property (nonatomic) UITapGestureRecognizer* tapRecognizer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.label = [[UILabel alloc] init];
    self.label.center = self.view.center;
    self.label.text = @"Hello TV!";
    self.label.textColor = [UIColor blackColor];
    self.label.font = [UIFont systemFontOfSize:90.0];
    [self.label sizeToFit];
    [self.view addSubview:self.label];
    
    UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
    tapRecognizer.allowedPressTypes = @[[NSNumber numberWithInteger:UIPressTypePlayPause] ];
    [self.view addGestureRecognizer:tapRecognizer];
    
}

- (void)tapped {
    self.label.textColor = [UIColor redColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我们使用手势而不是Button,是因为虽然tvOS app 的代码和iOS app的代码非常相似,但是交互的逻辑差别很大。我们稍晚一点会深入这一主题。


编译后结果是这样的:

HelloTV2


完整的工程见GitHub


原文在此

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值