ios resignFirstResponder 重置动画

标签 ios animation subview reset resignfirstresponder

首先为我糟糕的英语感到抱歉!!

在我的应用程序中有一个 UIViewController 并且 UIView 在 subViewLogin 中被称为 subViewLogin 有 2 个 UITextField 被称为用户名和密码。当我的应用程序运行时 subViewLogin 被 UIViewAnimationCurveLinear 移动到位置。但是当我完成编辑 UITextField(调用 resignFirstResponder)时 subViewLogin 卷土重来定位。为什么?我怎样才能防止 subViewLogin 重新定位?

这是我的代码。

//LoginViewController.h
#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UIImageView *background;
@property (weak, nonatomic) IBOutlet UIImageView *logoEnergy;
@property (weak, nonatomic) IBOutlet UIImageView *logoKmutl;
@property (weak, nonatomic) IBOutlet UIImageView *logoProjectname;
@property (weak, nonatomic) IBOutlet UIView *subviewLogin;

- (void)advoidSubviewFromKeyboard;
- (IBAction)usernameAndPasswordBeginEdit;
- (IBAction)dismissKeyboard;
@end

//LoginViewController.m
#import "LoginViewController.h"
@implementation LoginViewController

const float keyBoardPortraitHeight = 216;
const float keyBoardLandscapeHeight = 162;

@synthesize username, password, background, logoEnergy, logoKmutl, logoProjectname;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    UIView *subview = self.subviewLogin;
    subview.frame = CGRectMake(subview.frame.origin.x, self.view.frame.size.height, subview.frame.size.width, subview.frame.size.height);
}

- (void)viewDidAppear:(BOOL)animated
{
    [UIView
    animateWithDuration:0.3
    delay: 0.0
    options: UIViewAnimationCurveLinear
    animations:^{
        UIView *subview = self.subviewLogin;
        subview.frame = CGRectMake(subview.frame.origin.x, self.view.frame.size.height-subview.frame.size.height-30, subview.frame.size.width, subview.frame.size.height);
    }
    completion:^(BOOL finished){

    }
    ];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)advoidSubviewFromKeyboard
{
    [UIView
     animateWithDuration:0.3
     delay: 0.0
     options: UIViewAnimationCurveLinear
     animations:^{
         self.view.frame = CGRectMake(self.view.frame.origin.x, -keyBoardPortraitHeight+30, self.view.frame.size.width, self.view.frame.size.height);
     }
     completion:^(BOOL finished){

     }
     ];
}

- (IBAction)usernameAndPasswordBeginEdit
{
    [self advoidSubviewFromKeyboard];
}

- (IBAction)dismissKeyboard
{
    NSLog(@"dismiss");
    //[self.username becomeFirstResponder];
    //[self.username resignFirstResponder];
    [self.subviewLogin endEditing:YES];
    //[self.view endEditing:YES];
}
@end

最佳答案

即使很久以前就提出了这个问题,但这种行为现在仍然存在。我已经使用约束而不是更新元素的框架来解决它。示例:

func expand(with view: UIView, constraint: NSLayoutConstraint) {
    constraint.constant = self.isExpanded ? self.minHeight : self.maxHeight //set needed constant here
    UIView.animate(withDuration: expandDuration, animations: {
        self.arrowImageView.transform = self.isExpanded ? .identity : CGAffineTransform(rotationAngle: self.topAngle)
        view.layoutSubviews() //layout constraint's view subviews
    }) { _ in
        self.isExpanded = !self.isExpanded
    }
}

关于ios resignFirstResponder 重置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14091683/

相关文章:

javascript - 向右移动 div 动画不起作用

iphone - 触摸事件通过透明部分传递给superview

iphone - 数字键盘在完成显示时丢失自定义 UIButton 的奇怪问题

ipad - 如何在不使用常量的情况下获取分组的 UITableViewCell 内容区域宽度

ios - ios 中的应用内购买是否可以使用特定于帐户的订阅?

c# - 动画状态..无法播放是因为找不到?

ios - MyScript 如何将 mathwidget 添加为另一个 UIViewController 中的 subview

ios - Xcode iOS 14 WidgetKit 并不总是附加到调试器

iphone - UITableViewCell 字体大小没有改变

javascript - -webkit-transform 在 JavaScript 旋转动画中不起作用(无 JQuery)