目录
- UIDatePicker
- 如何暂停和恢复CALayer的动画
- requireGestureRecognizerToFail:
- URLEncode
- 用AVSpeechSynthesizer实现文本转语音
1.UIDatePicker
UIDatePicker在设置datePickerMode为UIDatePickerModeCountDownTimer时,当第一次valueChange的时候target的action不会调用,之后的valueChange都响应正常,解决代码如下:
1 2 3 4 5 6 7 8 9
| self.datePicker = [UIDatePicker new]; self.datePicker.datePickerMode = UIDatePickerModeCountDownTimer; self.datePicker.backgroundColor = [UIColor whiteColor]; [self.datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; //增加这三行代码 dispatch_async(dispatch_get_main_queue(), ^{ self.datePicker.countDownDuration = (NSTimeInterval) 1; }); [self addSubview:self.datePicker];
|
参考链接:http://stackoverflow.com/questions/20181980/uidatepicker-bug-uicontroleventvaluechanged-after-hitting-minimum-internal
2.如何暂停和恢复CALayer的动画:
示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| -(void)pauseLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; layer.speed = 0.0; layer.timeOffset = pausedTime; }
-(void)resumeLayer:(CALayer*)layer { CFTimeInterval pausedTime = [layer timeOffset]; layer.speed = 1.0; layer.timeOffset = 0.0; layer.beginTime = 0.0; CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; layer.beginTime = timeSincePause; }
|
参考链接:https://developer.apple.com/library/ios/qa/qa1673/_index.html
3.requireGestureRecognizerToFail:
UIView添加手势很常用,当添加两个或两个以上的手势时,需要使用requireGestureRecognizerToFail:方法来创建手势之间的依赖关系,常见的用法是当同时添加单击和双击手势时single-tap的手势会要求double-tap手势fail掉,示例如下:
1 2 3 4 5 6 7 8 9 10 11 12
| //添加单击手势 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSingleTapGestureHandled:)]; singleTap.numberOfTapsRequired = 1; singleTap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:singleTap]; //添加双击手势 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onDoubleTapGestureHandled:)]; doubleTap.numberOfTapsRequired = 2; doubleTap.numberOfTouchesRequired = 1; //注意是单击手势requireGestureRecognizerToFail:双击 [singleTap requireGestureRecognizerToFail:doubleTap]; [self.view addGestureRecognizer:doubleTap];
|
当单击和长按手势一起添加时,不需要调用requireGestureRecognizerToFail:方法,因为两者之间不会影响对方的state判断,如果调用了
1
| [longPressGesture requireGestureRecognizerToFail:singleTapGesture];
|
会导致长按手势响应时间变长,由默认的0.5秒变成1.5秒左右,而调用
1
| [singleTapGesture requireGestureRecognizerToFail:longPressGesture];
|
则不会出现这种情况。因为- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer
会导致otherGestureRecognizer延迟进入Possible状态,进而使得longPressGesture转换成Recognized的时间变长从而严重影响用户体验。哎,官方文档还是得多看啊!
4.URLEncode
跟web服务器打交道的时候,经常需要对URL进行Encode,不然可能出现各种error,下面以图片加载为例:
1 2 3 4
| NSString *urlString = @"http://s.checkin.tx2010.com/20160302175840/在路上.jpg"; NSURL *url = [NSURL URLWithString:urlString]; NSLog(@"url:%@",url); //打印结果url:(null)
|
因为没有对链接encode编码,导致用URLWithString:转成NSURL时对象为空,以至于用SDWebImage加载时图片显示不出来,修改逻辑如下:
1 2 3 4 5 6 7 8
| NSString *urlString = @"http://s.checkin.tx2010.com/20160302175840/在路上.jpg"; //注:iOS9已废弃stringByAddingPercentEscapesUsingEncoding:方法 //urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //此处使用URLQueryAllowedCharacterSet来满足需求 urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSURL *url = [NSURL URLWithString:urlString]; NSLog(@"url:%@",url); //打印结果url:http://s.checkin.tx2010.com/20160302175840/%E5%9C%A8%E8%B7%AF%E4%B8%8A.jpg
|
转换后的url正常,使用SDWebImage加载时图片也显示正常了,问题解决。
5.用AVSpeechSynthesizer实现文本转语音
1 2 3 4 5 6 7 8 9
| NSString *name = @"cocoa_chen你好"; AVSpeechSynthesizer *speechSynthesizer = [AVSpeechSynthesizer new]; AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:name]; //设置为中文语音 utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]; utterance.volume = 1.0f; //说话速率 utterance.rate = 0.5f; [speechSynthesizer speakUtterance:utterance];
|