自制定时器
简介
为了教学弟,写了一个定时器。只有简单的定时功能。根据你输入的数来定时,定时结束会播放一段音乐,并展示一张图片。我这里用的是动图,所以使用的是WebView来展示的。如果你想展示一张静态图或者想控制图片的播放时间,使用UIImageVIew就行。
代码
ViewController.m里的文件
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *Input;
@property (weak, nonatomic) IBOutlet UIButton *Play;
@property (weak, nonatomic) IBOutlet UIButton *Pause;
@property (weak, nonatomic) IBOutlet UILabel *Time;
@property (weak, nonatomic) IBOutlet UILabel *NameForApp;
@property (strong, nonatomic) NSTimer *timer1;
@property (strong, nonatomic) AVAudioPlayer *playMusic;
@property (strong, nonatomic) UIWebView *gifView;
@end
long int i , m , n;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_NameForApp.text = @"自制定时器";
_NameForApp.textColor = [UIColor blackColor];
_Time.text = @"00:00";
_Time.textColor = [UIColor blackColor];
_Input.placeholder = @"请输入记时时长";
_Input.clearButtonMode = UITextFieldViewModeWhileEditing;
_Input.clearsOnBeginEditing = NO;
[_Play setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[_Pause setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
_Pause.hidden = YES;
[_Play addTarget:self action:@selector(change) forControlEvents:UIControlEventTouchUpInside];
[_Pause addTarget:self action:@selector(change1) forControlEvents:UIControlEventTouchUpInside];
}
-(void)alert{
UIAlertController *AlertCtr = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入正确的时间" preferredStyle:UIAlertControllerStyleAlert];
[AlertCtr addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:AlertCtr animated:YES completion:nil];
}
-(void)change{
NSString *str1 = [NSString stringWithFormat:@"%@",_Input.text];
i = str1.integerValue;
if (i == 0) {
[self alert];
}
else {
_Play.hidden = YES;
_Pause.hidden = NO;
_Input.hidden = YES;
[self caculate];
_timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
[_timer1 setFireDate:[NSDate date]];
}
}
-(void)timeChange{
i--;
[self caculate];
if (i==0) {
[_timer1 invalidate];
_Play.hidden = NO;
_Pause.hidden = YES;
_Input.hidden = NO;
_Input.text = nil;
[self playdd];
[self showPicture];
}
}
-(void)showPicture{
_gifView = [[UIWebView alloc]initWithFrame:CGRectMake(55,150,220,254)];
[self.view addSubview:_gifView];
NSString *gifpath =[[NSBundle mainBundle]pathForResource:@"2222" ofType:@"gif"];
NSURL *gifUrl = [NSURL URLWithString:gifpath];
[_gifView loadRequest:[NSURLRequest requestWithURL:gifUrl]];
}
-(void)playdd{
NSString *apth = [[NSBundle mainBundle]pathForResource:@"1111" ofType: @"mp3"];
NSURL *url = [NSURL fileURLWithPath:apth];
self.playMusic = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
self.playMusic.volume =0.1;
self.playMusic.currentTime = 0;
[self.playMusic prepareToPlay];
[self.playMusic play];
}
-(void)caculate{
if (i >60) {
m = i / 60;
n =i % 60;
}
else
{
m=0;
n=i;
}
_Time.text = [NSString stringWithFormat:@"%02ld:%02ld",m,n];
}
-(void)change1{
_Play.hidden = NO;
_Pause.hidden = YES;
_Input.hidden = NO;
[_timer1 setFireDate:[NSDate distantFuture]];
_Input.text =[NSString stringWithFormat:@"%ld",i];
}
//全局代理
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
[self.playMusic stop];
_gifView.hidden = YES;
}
@end
音频文件与视频文件根据自己的需要导入。按钮图片也许要自己准备。
其他文件均没有改动。有问题可以提问哦。