1. 程式人生 > >iOS開發之微信聊天工具欄的封裝

iOS開發之微信聊天工具欄的封裝

//控制元件的初始化
-(void) addSubview
{
    self.voiceChangeButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [self.voiceChangeButton setImage:[UIImage imageNamed:@"chat_bottom_voice_press.png"] forState:UIControlStateNormal];
    [self.voiceChangeButton addTarget:self action:@selector(tapVoiceChangeButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.voiceChangeButton];
     
    self.sendVoiceButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [self.sendVoiceButton setBackgroundImage:[UIImage imageNamed:@"chat_bottom_textfield.png"] forState:UIControlStateNormal];
    [self.sendVoiceButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.sendVoiceButton setTitle:@"按住說話" forState:UIControlStateNormal];
     
     
    [self.sendVoiceButton addTarget:self action:@selector(tapSendVoiceButton:) forControlEvents:UIControlEventTouchUpInside];
    self.sendVoiceButton.hidden = YES;
    [self addSubview:self.sendVoiceButton];
     
    self.sendTextView = [[UITextView alloc] initWithFrame:CGRectZero];
    self.sendTextView.delegate = self;
    [self addSubview:self.sendTextView];
     
    self.changeKeyBoardButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [self.changeKeyBoardButton setImage:[UIImage imageNamed:@"chat_bottom_smile_nor.png"] forState:UIControlStateNormal];
    [self.changeKeyBoardButton
 addTarget:self action:@selector(tapChangeKeyBoardButton:) 
forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.changeKeyBoardButton];
     
    self.moreButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [self.moreButton setImage:[UIImage imageNamed:@"chat_bottom_up_nor.png"] forState:UIControlStateNormal];
    [self.moreButton addTarget:self action:@selector(tapMoreButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.moreButton];
     
    [self addDone];
     
     
     
    //例項化FunctionView
    self.functionView = [[FunctionView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
    self.functionView.backgroundColor = [UIColor blackColor];
     
    //設定資源載入的檔名
    self.functionView.plistFileName = @"emoticons";
     
    __weak __block ToolView *copy_self = self;
    //獲取圖片並顯示
    [self.functionView setFunctionBlock:^(UIImage *image, NSString *imageText)
     {
         NSString *str = [NSString stringWithFormat:@"%@%@",copy_self.sendTextView.text, imageText];
          
         copy_self.sendTextView.text = str;
          
         //把使用過的圖片存入sqlite
         NSData *imageData = UIImagePNGRepresentation(image);
         [copy_self.imageMode save:imageData ImageText:imageText];
     }];
     
     
    //給sendTextView新增輕擊手勢
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
    [self.sendTextView addGestureRecognizer:tapGesture];
     
     
    //給sendVoiceButton新增長按手勢
    UILongPressGestureRecognizer
 *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self 
action:@selector(sendVoiceButtonLongPress:)];
    //設定長按時間
    longPress.minimumPressDuration = 0.2;
    [self.sendVoiceButton addGestureRecognizer:longPress];
     
    //例項化MoreView
    self.moreView = [[MoreView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    self.moreView.backgroundColor = [UIColor blackColor];
    [self.moreView setMoreBlock:^(NSInteger index) {
        NSLog(@"MoreIndex = %d",(int)index);
    }];
     
}

4. 給我們的控制元件新增相應的約束,為了適合不同的螢幕,所以自動佈局是少不了的。當然啦給控制元件新增約束也必須是手寫程式碼啦,新增約束的程式碼如下:

//給控制元件加約束
-(void)addConstraint
{
    //給voicebutton新增約束
    self.voiceChangeButton.translatesAutoresizingMaskIntoConstraints = NO;
     
    NSArray *voiceConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_voiceChangeButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_voiceChangeButton)];
    [self addConstraints:voiceConstraintH];
     
    NSArray *voiceConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_voiceChangeButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_voiceChangeButton)];
    [self addConstraints:voiceConstraintV];
     
     
     
    //給MoreButton新增約束
    self.moreButton.translatesAutoresizingMaskIntoConstraints = NO;
     
    NSArray *moreButtonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_moreButton(30)]-5-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_moreButton)];
    [self addConstraints:moreButtonH];
     
    NSArray *moreButtonV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_moreButton(30)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_moreButton)];
    [self addConstraints:moreButtonV];
     
     
    //給changeKeyBoardButton新增約束
    self.changeKeyBoardButton.translatesAutoresizingMaskIntoConstraints = NO;
     
    NSArray *changeKeyBoardButtonH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[_changeKeyBoardButton(33)]-43-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_changeKeyBoardButton)];
    [self addConstraints:changeKeyBoardButtonH];
     
    NSArray *changeKeyBoardButtonV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[_changeKeyBoardButton(33)]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_changeKeyBoardButton)];
    [self addConstraints:changeKeyBoardButtonV];
     
     
    //給文字框新增約束
    self.sendTextView.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *sendTextViewConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-45-[_sendTextView]-80-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendTextView)];
    [self addConstraints:sendTextViewConstraintH];
     
    NSArray *sendTextViewConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_sendTextView]-10-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendTextView)];
    [self addConstraints:sendTextViewConstraintV];
     
     
    //語音傳送按鈕
    self.sendVoiceButton.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *sendVoiceButtonConstraintH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[_sendVoiceButton]-90-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendVoiceButton)];
    [self addConstraints:sendVoiceButtonConstraintH];
     
    NSArray *sendVoiceButtonConstraintV = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-6-[_sendVoiceButton]-6-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_sendVoiceButton)];
    [self addConstraints:sendVoiceButtonConstraintV];
     
     
}