亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS UIAlertController中UITextField添加晃動(dòng)效果與邊框顏色詳解

 更新時(shí)間:2017年10月19日 09:56:25   作者:iOS_ziank  
這篇文章主要給大家介紹了關(guān)于iOS UIAlertController中UITextField添加晃動(dòng)效果與邊框顏色的相關(guān)資料,實(shí)現(xiàn)后的效果非常適合在開(kāi)發(fā)中使用,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起看看吧。

前言

大家都知道在iOS8中引入了UIAlertController,通過(guò)UIAlertController可以方便的添加文本框進(jìn)行編輯,但是,在輸入錯(cuò)誤的內(nèi)容時(shí),如何對(duì)用戶(hù)進(jìn)行提醒就成了問(wèn)題,因?yàn)閁IAlertController中的所有UIAlertAction都會(huì)導(dǎo)致UIAlertController的消失。這里,我就描述兩種提示的方法,分別是晃動(dòng)文本框和修改邊框的顏色。下面話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的實(shí)現(xiàn)方法吧。

晃動(dòng)UITextField

晃動(dòng)UITextField其實(shí)就是對(duì)它添加一個(gè)動(dòng)畫(huà)效果,參考了Stack Overflow上的做法,通過(guò)添加position的動(dòng)畫(huà),可以實(shí)現(xiàn)UIAlertController中的UITextField的晃動(dòng)效果。

- (void)shakeField:(UITextField *)textField {
 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
 animation.duration = 0.07;
 animation.repeatCount = 4;
 animation.autoreverses = YES;
 animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(textField.centerX - 10, textField.centerY)];
 animation.toValue = [NSValue valueWithCGPoint:CGPointMake(textField.centerX + 10, textField.centerY)];
 [textField.layer addAnimation:animation forKey:@"position"];
}

修改UITextField的邊框顏色

UIAlertController中文本框的默認(rèn)邊框顏色都是黑色,通常在輸入異常時(shí)會(huì)改為紅色進(jìn)行提醒,這個(gè)時(shí)候,如果直接修改UITextField的border將會(huì)變成下圖樣式:

- (void)testAlert {
 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"測(cè)試" message:@"測(cè)試輸入框邊框顏色" preferredStyle:UIAlertControllerStyleAlert];
 [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  textField.layer.borderColor = [UIColor redColor].CGColor;
  textField.layer.borderWidth = 1;
 }];
 [self presentViewController:alert animated:YES completion:nil];
}


而在實(shí)際中我們應(yīng)該這樣修改:

- (void)testAlert {
 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"測(cè)試" message:@"測(cè)試輸入框邊框顏色" preferredStyle:UIAlertControllerStyleAlert];
 [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  self.currentField = textField;
 }];
 [self presentViewController:alert animated:YES completion:^{
  [[self.currentField superview] superview].backgroundColor = [UIColor redColor];
 }];
}

這樣的產(chǎn)生效果才是我們想要的。


需要注意的是:一定要在present以后進(jìn)行設(shè)置,否則會(huì)發(fā)現(xiàn)設(shè)置是無(wú)效的,因?yàn)闆](méi)有present之前,textField的superview是nil,設(shè)置是無(wú)效的。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,本文還有許多不足,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論