Oculus Reparo 允许您使用纯文本文件定义视图布局。
让我们构建一个简单的动画交通灯
将以下文本文件包含在您的应用程序主捆绑包中
/* Let's define some variables */
@set red: #ff3b30;
@set amber: #ff9500;
@set green: #4cd964;
@set grey: #8e8e93;
/* Add a UIView */
view {
position @if portrait { /* Position if portrait */
align: center middle;
height: 300;
width: 80;
}
position @if landscape { /* Position if landscape */
align: center middle;
height: 80;
width: 300;
}
/* Define a mixin */
@define: traffic-light {
/* Add a CALayer */
layer {
position {
top: @position @if portrait;
left: 0 @if portrait;
top: 0 @if landscape;
left: @position @if landscape;
width: 80;
height: 80;
}
id: @id;
background-color: @color;
corner-radius: 40;
opacity: 0.5;
}
}
/* Include red light... */
@traffic-light {
id: red;
position: 0;
color: @red;
}
/* Include amber light */
@traffic-light {
id: amber;
position: +20;
color: @amber;
}
/* Include green light */
@traffic-light {
id: green;
position: +20;
color: @green;
}
}
/* Add a UIButton */
button {
/* Set it's position */
position {
top: 20 @if portrait; /* Let's move the button depending */
top: 0 @if landscape; /* on the screen's orientation */
left: 0;
width: 100;
height: 44;
}
font-size: 17; /* Font parameters */
font-weight: regular;
text-alignment: left;
title: Back; /* Title */
title-color: @grey;
on-touch: onBack; /* Objective C Selector */
image-bundle: BackwardDisclosure22x22; /* Image parameters */
tint-color: @grey;
image-edge-insets: 0 7.5 0 7.5;
}
已经提供了一个 LayoutViewController 以帮助您渲染视图,但是如果您想完全控制屏幕大小和方向变化,您也可以自己管理视图的生命周期。
import UIKit
import OculusReparo
class TrafficLightsController : LayoutViewController {
var red: CALayer? // Mapped automatically from our view
var amber: CALayer?
var green: CALayer?
override func viewWillLayout() {
layout.filename = "TrafficLights.layout" // The name of our text file
layout.model = self // Sets object to map to
}
override func viewDidLayout() {
// Start a timer
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(onTimer), userInfo: nil, repeats: true)
}
// Invoked from the view
func onBack() {
navigationController?.popViewControllerAnimated(true)
}
// Animate our lights
func onTimer(timer: NSTimer) {
if red?.opacity == 1 {
red?.opacity = 0.5
amber?.opacity = 1
}
else if amber?.opacity == 1 {
amber?.opacity = 0.5
green?.opacity = 1
}
else {
green?.opacity = 0.5
red?.opacity = 1
}
}
}
OculusReparo 通过 CocoaPods 提供。要安装它,请简单地将以下行添加到您的 Podfile 中
pod "OculusReparo"