试图在WatchKit中获取旋转按钮

时间:2015-06-23 22:15:30

标签: watchkit

我在iPhone应用程序中使用swift的代码很好但在WatchKit 7.0 beta中却没有。出口和行动是不同的。我不确定需要改变哪些才能使它在WatchKit中运行。请帮忙!

import WatchKit
import Foundation

class InterfaceController: WKInterfaceController {

    @IBOutlet var spinButton: WKInterfaceButton!

    var isRotating = false


    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)

        // Configure interface objects here.
    }

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user
        super.willActivate()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    @IBAction func spinAction() {

        if !isRotating {
        // create a spin animation
        let spinAnimation = CABasicAnimation()
        // starts from 0
        spinAnimation.fromValue = 0
        // goes to 360 ( 2 * π )
        spinAnimation.toValue = M_PI*2
        // define how long it will take to complete a 360
        spinAnimation.duration = 1
        // make it spin infinitely
        spinAnimation.repeatCount = Float.infinity
        // do not remove when completed
        spinAnimation.removedOnCompletion = false
        // specify the fill mode
        spinAnimation.fillMode = kCAFillModeForwards
        // and the animation acceleration
        spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        // add the animation to the button layer
        spinButton.layer.addAnimation(spinAnimation, forKey: "transform.rotation.z")

    } else {
        // remove the animation
        spinButton.layer.removeAllAnimations()
    }
    // toggle its state
    isRotating = !isRotating

  }

}

1 个答案:

答案 0 :(得分:1)

在开发watchOS时,您仅限于iOS上可用的所有API的子集。

如果你想做基本的动画,请尝试使用WKInterfacePicker,并在移动数字表冠时更改图像。

IBOutlet WKInterfacePicker *myPicker;

- (void)willActivate {
    [super willActivate];

    WKPickerItem *item1 = [[WKPickerItem alloc] init];
    item1.contentImage = [WKImage imageWithImageName:@"Unknown.png"];

    WKPickerItem *item2 = [[WKPickerItem alloc] init];
    item2.contentImage = [WKImage imageWithImageName:@"Unknown-2.png"];

    [self.myPicker setItems:array];

}

当值超过数组计数时,从索引0开始。

- (IBAction)myPickerAction:(NSInteger)value {

    if (value % 2 == 0) {
        [self.myPicker setSelectedItemIndex:-1];
    }

}

这将使数字表冠旋转时WKInterfacePicker在图像之间发生变化。