分析 imitate wechat - 1 中遇到的学习点:
一. 委托,传参
1.
委托,关于委托的知识点在我博客的文章也说了很多就不再阐述了,主讲一套“规范”:
委托者对象中定义 delegate 委托指针 :
@interface KeyBordView : UIView
@property (nonatomic, assign) id <KeyBordVIewDelegate> delegate;
@end
#pragma mark - UITextField代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(KeyBordView:textFiledBegin:)])
{
[self.delegate KeyBordView:self textFiledBegin:textField];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([self.delegate respondsToSelector:@selector(KeyBordView:textFiledReturn:)])
{
[self.delegate KeyBordView:self textFiledReturn:textField];
}
return YES;
}
在 被委托者 中完成 委托对象指针赋值,并完成委托方法,当然这里还没开始实现呢:
self.keyBordView.delegate = self;
#pragma mark - KeyBordViewDelegate
- (void)beginRecord
{
}
- (void)finishRecord
{
}
- (void)KeyBordView:(KeyBordView *)keyBoardView textFiledBegin:(UITextField *)textFiled
{
}
- (void)KeyBordView:(KeyBordView *)keyBoardView textFiledReturn:(UITextField *)textFiled
{
}
二. 点击键盘弹上,轻滑界面键盘还原
1.
注册键盘通知和相应操作,注册了这两个通知之后,其中willShow的通知会根据view上的UITextField成为第一响应者后马上触发
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - NSNotificationCenter Operation
- (void)keyboardWillShow:(NSNotification* )noti
{
CGRect keyBoardRect = [noti.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat deltaY = keyBoardRect.size.height;
[UIView animateWithDuration:[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, -deltaY);
}];
}
- (void)keyboardWillHide:(NSNotification* )noti
{
[UIView animateWithDuration:[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
调用 endEditing ,endEditing方法会强制令view上的所有UITextField resign掉第一响应者,当resign掉第一响应者的时候马上会出发键盘的全局通知,从而收回键盘
#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.view endEditing:YES];
}
三. 各种技巧
1.
UIScrollView 的 contentView 的Masonry实现是可以指定为常量的
UIView* superview = self.view;
UIScrollView* scrollView = [UIScrollView new];
scrollView.backgroundColor = [UIColor lightGrayColor];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.bounds.size.width, 800);
[scrollView flashScrollIndicators];
[self.view addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker* make){
make.edges.equalTo(superview).with.insets(UIEdgeInsetsMake(0, 0, -44, 0));
}];
2.
页面的平移用属性transform,恢复原值更容易,有参数实现
向上平移deltaY个单位
self.view.transform = CGAffineTransformMakeTranslation(0, -deltaY);
self.view.transform = CGAffineTransformIdentity;