本文属于NilStack 写作的《Apple TV app 开发教程》的第一篇。翻译讲究信达雅,因为时间和水平有限,部分地方在自己理解的基础上翻译的。更多文章请访问本人在GitHub 的博客。
苹果公司发布tvOS 和新一代Apple TV 后,Apple TV 成了另一个开发平台。
程序员要做的第一件事,自然是开发一个“Hello Apple TV!”程序。
先从苹果开发者网站上下载Xcode 7.1 beta 并安装。启动Xcode 后,在选择模板的界面,可以看到增加了一个tvOS 分类,选择“Single View Application”。
接下来的步骤和创建iOS 应用是一模一样的,生成的文件你应该也很熟悉。
我们在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的代码非常相似,但是交互的逻辑差别很大。我们稍晚一点会深入这一主题。
编译后结果是这样的:
完整的工程见GitHub。
原文在此。