1

the code i'm using works just fine in swift for iPhone apps but not in the WatchKit 7.0 beta. the outlets and actions are different. I'm not sure what needs to change to make it work in WatchKit. please help!

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

  }

}
Dai
  • 141,631
  • 28
  • 261
  • 374
Chris Hageman
  • 111
  • 1
  • 8

1 Answers1

1

You are limited to a subset of all the APIs available on iOS when developing for the watchOS.

If you want to do basic animations try out a WKInterfacePicker and change images when the digital crown is moved.

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];

}

When the value exceeds the array count start over from index 0.

- (IBAction)myPickerAction:(NSInteger)value {

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

}

This will make the WKInterfacePicker change between your images when the digital crown is rotated.

Philip
  • 2,287
  • 1
  • 22
  • 37
  • Thank you but that's not what I'm trying to do. I want a button with an image to spin when pushed and stop spinning when pushed again. Is that possible? Why is it so limited with the watchOS? – Chris Hageman Jun 24 '15 at 18:55
  • You could add an image sequence to a button and simulate rotation that way. Im afraid you can't use CABasicAnimation on WKInterface elements – Philip Jun 26 '15 at 07:53
  • @ChrisHageman you ever found a solution? Apparently you can animate some things, see [here](https://developer.apple.com/documentation/watchkit/wkinterfacecontroller/1628239-animate). However, not sure how to use it. – Jonas Sourlier Nov 12 '21 at 11:00