按下0.5秒按钮,播放一半的动画和声音?(经过编辑,更好地传达))



在我的应用程序中,你按下一个按钮,它就会播放动画帧1,2,3,4,5,6,7,8,9,10。

然而,我想要的基本上是按下按钮0.2秒播放第1、2、3帧,但按下按钮0.5秒播放第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

(这不是转发,很抱歉造成混淆,这篇文章更专注于动画,表达得更好。

首先声明一个类属性来保存您的时间。

然后:

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
}

最新更新