iOS监听键盘改变view的frame

将view关联成该类,并把输入框放在该view上.

//
//  KeyboardListenerView.m
//  ParkingProject
//
//  Created by Eric on 16/10/12.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "KeyboardListenerView.h"

@implementation KeyboardListenerView


//记录键盘是否移动过;
static bool isMove;
//记录键盘上移的距离
static CGFloat subHeight;
- (void)drawRect:(CGRect)rect {
    // 开始监听
    [self keyBoardNosnotification];

}
- (void)keyBoardNosnotification {
    //监听键盘状态
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardShow:(NSNotification *)notification
{
    //键盘的frame
    CGRect rect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //键盘的y
    CGFloat y = rect.origin.y;
    [UIView animateWithDuration:0.2 animations:^{
        //view的最大y值
        CGFloat maxY = CGRectGetMaxY(self.frame);
        //比较view和键盘的y判断是否需要移动
        isMove = maxY > y ? YES:NO;
            if (isMove) {
                //移动的距离
                subHeight = maxY-y;
                //移动,注意不能直接改frame;
                self.transform = CGAffineTransformMakeTranslation(0, self.transform.ty-subHeight);
            }
    }];
    
}

- (void)keyboardHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.1 animations:^{
        //如果移动了,移动回来.
        if (isMove) {
            self.transform =CGAffineTransformMakeTranslation(0,self.transform.ty+subHeight);

        }
    }];
}
@end

 

转载于:https://www.cnblogs.com/sunmair/p/5953986.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 iOS 中的 UIView 类来创建一个可移动和可改变尺寸的视图。你可以使用 UIPanGestureRecognizer 和 UIPinchGestureRecognizer 来实现移动和缩放的手势识别。 以下是一个示例代码,可以创建一个可移动和可改变尺寸的视图: ```swift class ResizableView: UIView { required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) setupGestureRecognizer() } override init(frame: CGRect) { super.init(frame: frame) setupGestureRecognizer() } private func setupGestureRecognizer() { let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:))) self.addGestureRecognizer(panGestureRecognizer) let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(handlePinchGesture(_:))) self.addGestureRecognizer(pinchGestureRecognizer) } @objc func handlePanGesture(_ gestureRecognizer: UIPanGestureRecognizer) { let translation = gestureRecognizer.translation(in: self.superview) center = CGPoint(x: center.x + translation.x, y: center.y + translation.y) gestureRecognizer.setTranslation(CGPoint.zero, in: self.superview) } @objc func handlePinchGesture(_ gestureRecognizer: UIPinchGestureRecognizer) { if gestureRecognizer.state == .changed { let scale = gestureRecognizer.scale transform = transform.scaledBy(x: scale, y: scale) gestureRecognizer.scale = 1.0 } } } ``` 你可以在你的视图控制器中使用以下代码来创建并添加这个视图: ```swift let resizableView = ResizableView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) resizableView.backgroundColor = UIColor.red self.view.addSubview(resizableView) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值