xcode 如何在 Apple WatchKit 中制作动画按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27422643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:21:08  来源:igfitidea点击:

How to make an animated button in Apple WatchKit?

iosobjective-cxcodewatchkit

提问by Reck Hou

I'd like to make a button with animation in WatchKit, but it seems I can't find a way to modify WKInterfaceButtonor drag an image into button in storyboard. Any help is appreciated.

我想在 WatchKit 中制作一个带有动画的按钮,但似乎我找不到一种方法来修改WKInterfaceButton或拖动图像到故事板中的按钮中。任何帮助表示赞赏。

回答by Reck Hou

After some research I found the solution, it's pretty simple but easily ignored :)

经过一番研究,我找到了解决方案,它非常简单但很容易被忽略:)

First, drag a button into a scene created in storyboard.

首先,将一个按钮拖到在故事板中创建的场景中。

Second, select button, modify its property contentfrom Textto Group. If you can't find contentproperty, click the Attributes Inspectorbutton at top right of your screen, it looks like a breakpoint button or a down arrow with a bar.

其次,选择按钮,修改其属性contentTextGroup。如果找不到content属性,请单击Attributes Inspector屏幕右上角的按钮,它看起来像一个断点按钮或带有条形的向下箭头。

enter image description here

在此处输入图片说明

Now you can control group created inside your button. You need to add a reference of this WKInterfaceGroupinside your controller code. Here is an example:

现在您可以控制在按钮内创建的组。您需要WKInterfaceGroup在控制器代码中添加 this 的引用。下面是一个例子:

@interface AAPLButtonDetailController()
@property (weak, nonatomic) IBOutlet WKInterfaceGroup *buttonGroup;   
@end


@implementation AAPLButtonDetailController

- (instancetype)init {
    self = [super init];

    if (self) {
        // Initialize variables here.
        // Configure interface objects here.
        [_buttonGroup setBackgroundImageNamed:@"Bus"];
        [_buttonGroup startAnimating];
    }

    return self;
}

So the button animation will be played after scene initialized. Remember only frame animation is supported for now, all frames in one animation should be named like "Bus0.png", "Bus1.png"....

所以按钮动画会在场景初始化后播放。请记住,目前仅支持帧动画,一个动画中的所有帧都应命名为“Bus0.png”、“Bus1.png”....

enter image description here

在此处输入图片说明

Hope this can help :)

希望这可以帮助:)

回答by DenVog

Reck's post worked for me in instances that I needed to control the repeat count or duration. If you simply want to start or stop an animation for a button, the following works for me, where twoButton is a WKInterfaceButton and I have a set of images name [email protected], [email protected], etc.

在我需要控制重复次数或持续时间的情况下,Reck 的帖子对我有用。如果您只是想开始或停止按钮的动画,以下对我有用,其中 twoButton 是 WKInterfaceButton 并且我有一组图像名称 [email protected][email protected] 等。

- (void)startButtonAnimating
{
    // This works, but can't control repeat counts or duration
    [self.twoButton setBackgroundImageNamed:@"Bus"];
    // But you can stop after a delay by setting background image nil
    [self performSelector:@selector(stopGroupButtonAnimating) withObject:nil afterDelay:1.5];
}

- (void)stopButtonAnimating
{
    [self.twoButton setBackgroundImage:nil];
}