当前位置: 首页 > 工具软件 > imitate-One > 使用案例 >

imitate wechat - 2

田英卓
2023-12-01

分析  imitate wechat - 1  中遇到的学习点:


一. 委托,传参

1.

委托,关于委托的知识点在我博客的文章也说了很多就不再阐述了,主讲一套“规范”:

委托者对象中定义  delegate 委托指针 :

@interface KeyBordView : UIView

@property (nonatomic, assign) id <KeyBordVIewDelegate> delegate;

@end


2.
在委托者当中的某个方法(这里是UITextFiled的委托方法中,具体看逻辑) 判断  被委托者(此时如果完成委托,两者的指针是同一个) 实现了委托方法了吗?如果实现,完成对象间或者说页面间的 “传值”, 不过,说传参更好!

#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;
}


3.

在 被委托者 中完成 委托对象指针赋值,并完成委托方法,当然这里还没开始实现呢:

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;
    }];
}

2.

调用 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;



 类似资料: