1.Lable控件圆角设置
.h文件:(需提前添加QuartzCore.framework的引用)
#import<QuartzCore/QuartzCore.h>
.m文件:
- (void) viewWillAppear:(BOOL)animated
{
[lblTimer.layersetCornerRadius:5.0];
[lblTitle.layersetCornerRadius:5.0];
// ...
}
2.应用设置Settings Bundle
新建文件Settings bundle,如下图:
打开Settings bundle的Root.plist文件进行设置,如下图:
对应的效果图:
使用方法:
- (void) viewWillAppear:(BOOL)animated
{
// ...
//获取用户应用参数设置
NSUserDefaults *settings = [NSUserDefaultsstandardUserDefaults];
timeSettings = [[settingsobjectForKey:@"timeSettings_preference"]intValue];
bool autoStart = [[settings objectForKey:@"autoStart_preference"] boolValue];
// ...
}
注意事项:如果用户在手机系统设置中没有打开过TimerDown的设置,则这些值将不会设置上,获取时为nil,所以必须初始化这些值,方法如下:
打开AppDelegate.m文件,在applicationFinishLaunching:方法中添加:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// ...
//初始化应用设定,解决用户没有在系统设置当中进行应用参数设置,导致程序取应用设置值为nil
NSUserDefaults *settings = [NSUserDefaultsstandardUserDefaults];
NSObject *timeSettings = [settings objectForKey:@"timeSettings_preference"];
if (nil == timeSettings)
{
NSDictionary *appDefaluts = [NSDictionarydictionaryWithObject:@"5"forKey:@"timeSettings_preference"];
[settings registerDefaults:appDefaluts];
[settings synchronize];
}
// ...
}
NSUserDefaults中不仅仅可以存放应用设置中的数据,它还能存放其它数据,如上次用户登录时间等等。
3.设置应用图标
将准备好尺寸为57*57的图片icon.png,29*29的图片icon-Small.png拖放至项目中(我放在Supporting Files中,其它目录也行)
打开TimeDown-Info.plist文件,设置Icon files项(如果没有该项,可以通过右键,选择弹出菜单Add Row来添加),如下图:
4.利用计时器来重复刷新UI
- (void) viewWillAppear:(BOOL)animated
{
// ...
// timeSettings为分,此处每秒进行一次刷新; 使用NStimer 设置间隔1.0秒执行一次回调函数oneSecond:
timeSettings *=60;
[lblTimer setText:@""];
if (autoStart)
setTimer = [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(oneSecond:)userInfo:nilrepeats:YES];
// ...
}
回调函数:
- (void) oneSecond:(NSTimer *)timer
{
timeSettings--;
if (timeSettings > -1) {
[lblTimersetText:[NSStringstringWithFormat:@"%d:%02d",timeSettings/60,timeSettings%60]];
}else {
[timer invalidate];
timer = nil;
[selfplaySound:@"beeps"];
[selfvibrate];
}
}
5.播放音频和设置振动
使用AVFoundation播放音频,AudioToolbox设置振动,将准备好的音频文件beeps.mp3拖放至项目中。
.h文件:(需提前添加AVFoundation.framework/AudioToolbox.framework的引用)
#import<AVFoundation/AVFoundation.h>
#import<AudioToolbox/AudioToolbox.h>
@property (retain,nonatomic)AVAudioPlayer *aAudioPlay;
.m文件:
@synthesize aAudioPlay;
-(void) playSound:(NSString *)fileName
{
NSString *sFileName = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
if(nil == aAudioPlay)
{
[aAudioPlaystop];
//[aAudioplay Release];//如果启用ARC,则不用手动回收
}
aAudioPlay = [[AVAudioPlayeralloc]initWithContentsOfURL:[NSURLfileURLWithPath:sFileName]error:NULL];
[aAudioPlayplay];
}
- (void) vibrate
{
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
6.检测并处理手机摇晃动作
有两种处理方式:
一种是ShakeToEdit方法,通过重写UIResponder类下手势方法(可以用模拟器测试)
首先,打开AppDelegate.m文件,在applicationFinishLaunching:方法中添加:
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
// ...
//允许支持ShakeToEdit
application.applicationSupportsShakeToEdit =YES;
// ...
}
或者,在view控制器.m文件viewDidload方法中添加:
- (void)viewDidLoad
{
[superviewDidLoad];
//允许支持ShakeToEdit
[[UIApplicationsharedApplication]setApplicationSupportsShakeToEdit:YES];
}
然后重写以下方法:
- (BOOL) canBecomeFirstResponder
{
return YES;
}
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[selfbecomeFirstResponder];
}
- (void) viewWillDisappear:(BOOL)animated
{
[selfresignFirstResponder];
[super viewWillDisappear:animated];
}
-(void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion ==UIEventSubtypeMotionShake)
{
//NSLog(@"shaking~~~~~~~~~");
//...
}
}
另一种利用加速计和陀螺仪方法(只能真机测试),此方法又细分为两种:
UIAccelerometer,ios5以后不推荐使用。
.h文件:
@interface UXViewController :UIViewController <UIAccelerometerDelegate>
// ...
@property (retain,nonatomic)UIAcceleration *prevAcceleration;
// ...
@end
.m文件:
@synthesize prevAcceleration;
- (void) viewWillAppear:(BOOL)animated
{
// ...
// 设置共享的加速计的delegate
if ([UIAccelerometer sharedAccelerometer].delegate == nil) {
[UIAccelerometer sharedAccelerometer].delegate = self;
[UIAccelerometer sharedAccelerometer].updateInterval = 1.0f / 60.0f;//加速计更新频率,以秒为单位
}
// ...
}
- (BOOL) shakingEnoughFromPrev:(UIAcceleration *)prevShake toThisShake:(UIAcceleration *)thisShake withThisThreshold:(double) shakeThreshold
{
double dX = fabs(prevShake.x - thisShake.x);
double dY = fabs(prevShake.y - thisShake.y);
double dZ = fabs(prevShake.z - thisShake.z);
return (dX > shakeThreshold && dY > shakeThreshold) || (dX > shakeThreshold && dZ > shakeThreshold) || (dZ > shakeThreshold && dY > shakeThreshold);
}
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if (self.prevAcceleration) {
if ([selfshakingEnoughFromPrev:self.prevAccelerationtoThisShake:accelerationwithThisThreshold:0.5 ])
{
//NSLog(@"shaking~~~~~~~~~");
// ...
}
}
self.prevAcceleration = acceleration;
}
CoreMotion,ios4及更高版本使用。
.h文件:(需提前添加Coremotion.framework的引用)
#import<CoreMotion/CoreMotion.h>
@property (retain,nonatomic)CMMotionManager *motionManager;//当前应用只能有一个CMMotionManager实例,多个实例会影响接收速率
.m文件:
@synthesize motionManager;
- (void) viewWillAppear:(BOOL)animated
{
// ...
//初始化加速计
self.motionManager = [[CMMotionManageralloc]init];
self.motionManager.accelerometerUpdateInterval =.1; //加速计更新频率,以秒为单位
// ...
}
- (void) viewDidAppear:(BOOL)animated
{
// ...
//以push的方式更新并在block中接收加速度
[self.motionManagerstartAccelerometerUpdatesToQueue:[[NSOperationQueuealloc]init]withHandler:^(CMAccelerometerData *accelerometerData,NSError *error)
{
[selfupdateAccelerationData:accelerometerData];
if (error)
{
NSLog(@"motion error:%@",error);
}
}
];
//以pull的方式更新并在block中接收加速度
[self.motionManagerstartAccelerometerUpdates];
NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:0.2ftarget:selfselector:@selector(updateAcceleration:)userInfo:nilrepeats:YES];
[timer fire];
// ...
}
- (void) updateAcceleration:(id)userInfo
{
CMAccelerometerData *accelerometerData =self.motionManager.accelerometerData;
[selfupdateAccelerationData:accelerometerData];
}
- (void) updateAccelerationData:(CMAccelerometerData *)accelerometerData
{
double x = accelerometerData.acceleration.x;
double y = accelerometerData.acceleration.y;
double z = accelerometerData.acceleration.z;
if (fabs(x) > 2.0 || fabs(y) > 2.0 || fabs(z) > 2.0)
{
//停止更新加速计
[self.motionManagerstopAccelerometerUpdates];
dispatch_async(dispatch_get_main_queue(), ^{
//UI线程必须在此block内执行,例如摇一摇动画、UIAlertView之类
NSLog(@"检测到摇晃");
});
}
}
7.Action Sheet使用方法
.h文件:
@interface UXViewController :UIViewController <UIActionSheetDelegate>
.m文件:
在需要弹出ActionSheet的地方加入以下代码:
// 弹出ActionSheet
UIActionSheet *as = [[UIActionSheetalloc]initWithTitle:@"Start Timer"delegate:selfcancelButtonTitle:@"Cancel"destructiveButtonTitle:nilotherButtonTitles:@"Start",nil];
[as showInView:self.view];
添加ActionSheet响应函数:
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (0 == buttonIndex) {
// 点击ActionSheet中“Start”按钮
}
}