全局异步和主线程异步区别、改变PlaceHolder颜色、解决键盘弹起挡住文本框问题...

本文分享了使用Swift进行UI开发的多个实用技巧,包括如何全局异步执行耗时任务,主线程异步刷新UI,改变PlaceHolder颜色,解决键盘弹起挡住文本框的问题以及通过控件Y坐标计算View显示范围等,旨在提升开发效率和用户体验。

1、全局异步执行耗时任务

dispatch_async(dispatch_get_global_queue(0, 0), ^{

});

2.主线程异步刷新UI

dispatch_async(dispatch_get_main_queue(), ^{

});

3.改变PlaceHolder的颜色

[username_text setValue:[UIColor colorWithRed:1 green:1

blue:1 alpha:0.5]

forKeyPath:@"_placeholderLabel.textColor"];

4.解决键盘弹起挡住文本框的问题

//键盘弹起时重置View大小

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent

*)event{

[username_text resignFirstResponder];

[password_text resignFirstResponder];

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard"context:nil];

[UIView setAnimationDuration:animationDuration];

CGRect rect = CGRectMake(0.0f, 0.0f,self.view.frame.size.width,self.view.frame.size.height);

self.view.frame = rect;

[UIView commitAnimations];

}

 

 

//退出编辑(键盘落下)重置View大小

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard"context:nil];

[UIView setAnimationDuration:animationDuration];

CGRect rect = CGRectMake(0.0f, 0.0f,self.view.frame.size.width,self.view.frame.size.height);

self.view.frame = rect;

[UIView commitAnimations];

[textField resignFirstResponder];

return YES;

}

 

5.开始编辑(键盘弹起)通过控件Y坐标计算View显示范围

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

CGRect frame = password_text.frame;

int offset = frame.origin.y + 32 -

(self.view.frame.size.height - 216.0);

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyBoard"

context:nil];

[UIView setAnimationDuration:animationDuration];

floatwidth =self.view.frame.size.width;

floatheight =self.view.frame.size.height;

if(offset > 0)

{

CGRect rect = CGRectMake(0.0f, -

offset,width,height);

self.view.frame = rect;

}

[UIView commitAnimations];

}

转载于:https://www.cnblogs.com/CJH5209/p/6072129.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值