0.5 秒按钮按下,播放一半的动画和声音? (编辑,更好地传达))
Posted
技术标签:
【中文标题】0.5 秒按钮按下,播放一半的动画和声音? (编辑,更好地传达))【英文标题】:0.5 Second Button Pressed, Play half of animation and sound? (edited, better conveyed)) 【发布时间】:2014-03-04 23:46:41 【问题描述】:在我的应用中,您按下一个按钮,它会播放动画帧 1、2、3、4、5、6、7、8、9、10。
但是我想要的基本上是按下按钮 0.2 秒播放第 1、2、3 帧,但按下 0.5 秒播放第 1、2、3、4、5、6 帧等...播放的帧越多按住按钮的时间越长。我想要当你按下同一个按钮时播放的声音,即播放前 0.2 秒的声音,按下按钮 0.2 秒等。
我真的很难找到一个好的教程,因为我只是一个初学者。所以请帮忙,但记得用尽可能多的说明来解释。这是我的代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)Button
AudioservicesPlaySystemSound(SoundID);
-(IBAction)startanimating
animatedimage.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"frames_00.png"],
[UIImage imageNamed:@"frames_05.png"],
[UIImage imageNamed:@"frames_10.png"],
[UIImage imageNamed:@"frames_15.png"],
[UIImage imageNamed:@"frames_20.png"],
[UIImage imageNamed:@"frames_25.png"],
[UIImage imageNamed:@"frames_30.png"],
[UIImage imageNamed:@"frames_35.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_45.png"],
[UIImage imageNamed:@"frames_50.png"],
[UIImage imageNamed:@"frames_50.png"],
[UIImage imageNamed:@"frames_45.png"],
[UIImage imageNamed:@"frames_45.png"],
[UIImage imageNamed:@"frames_45.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_40.png"],
[UIImage imageNamed:@"frames_35.png"],
[UIImage imageNamed:@"frames_35.png"],
[UIImage imageNamed:@"frames_35.png"],
[UIImage imageNamed:@"frames_30.png"],
[UIImage imageNamed:@"frames_30.png"],
[UIImage imageNamed:@"frames_30.png"],
[UIImage imageNamed:@"frames_25.png"],
[UIImage imageNamed:@"frames_25.png"],
[UIImage imageNamed:@"frames_25.png"],
[UIImage imageNamed:@"frames_25.png"],
[UIImage imageNamed:@"frames_25.png"],
[UIImage imageNamed:@"frames_25.png"],
[UIImage imageNamed:@"frames_20.png"],
[UIImage imageNamed:@"frames_20.png"],
[UIImage imageNamed:@"frames_20.png"],
[UIImage imageNamed:@"frames_15.png"],
[UIImage imageNamed:@"frames_15.png"],
[UIImage imageNamed:@"frames_15.png"],
[UIImage imageNamed:@"frames_10.png"],
[UIImage imageNamed:@"frames_10.png"],
[UIImage imageNamed:@"frames_10.png"],
[UIImage imageNamed:@"frames_05.png"],
[UIImage imageNamed:@"frames_05.png"],
[UIImage imageNamed:@"frames_05.png"],
[UIImage imageNamed:@"frames_00.png"],nil];
[animatedimage setAnimationRepeatCount:1];
animatedimage.animationDuration = 0.7;
[animatedimage startAnimating];
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *buttonURL = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"AVTREV" ofType:@"mp3"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)buttonURL, &SoundID);
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
我也听说我应该为此使用 CALayers 和 CAanimation,这样会更好吗?如果是这样,请提供有关如何使用 CA 以及如何实现按钮持续时间的帮助。谢谢您,我将非常感谢您的帮助! :D
(这不是转发,很抱歉造成混乱,这篇文章更侧重于动画并且更好地表达。
【问题讨论】:
【参考方案1】:首先声明一个类属性来保持你的时间。
然后:
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100.0, 100.0, 100.0, 20.0);
[myButton setTitle:@"Test" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(userEndsTap:) forControlEvents:UIControlEventTouchUpInside];
[myButton addTarget:self action:@selector(userStartsTap:) forControlEvents:UIControlEventTouchDown];
- (void)userStartsTap:(id)sender
// start time measurement
- (void)userEndsTap:(id)sender
// stop time measurement, get duration and execute code according to time passed
【讨论】:
谢谢你,但是我真的不知道把这行代码放在哪里以及确切地替换什么?如果您不介意,请提供更多指导。谢谢! UIButton代码可以放在viewDidLoad中。这是一种以编程方式创建按钮的方式。或者,您可以跳过按钮创建代码并使用界面构建器将 userStarts 和 userEnds 连接到同一个按钮。如果你这样做,不要忘记将 void 更改为 IBAction。以上是关于0.5 秒按钮按下,播放一半的动画和声音? (编辑,更好地传达))的主要内容,如果未能解决你的问题,请参考以下文章