2016-12-12 33 views
0

我正在嘗試對UITextView進行動畫處理,以將其擴展爲包含UIToolbar的寬度。但它動畫怪異,跳來跳去。在UIToolbar問題中爲UITextview設置動畫效果

enter image description here

誰能幫我明白爲什麼我的文本框周圍跳躍?

#define ANIMATION_DURATION 0.5 
#define ANIMATION_DELAY 0.03 
    @interface WebViewController() <UITextFieldDelegate>{ 
     CGRect URLBAR_DEFAULT; 
    } 

    -(void)viewDidLoad{ 
    self.txtURLBar = [[UITextField alloc] init]; 
     self.txtURLBar.bounds = CGRectMake(0, 0, 100, 30); 
     self.txtURLBar.delegate = self; 
     self.txtURLBar.backgroundColor = [UIColor lightGrayColor]; 
     self.txtURLBar.clearButtonMode = UITextFieldViewModeWhileEditing; 


     URLBAR_DEFAULT = self.txtURLBar.frame; //KEEP A REFRENCE TO THE ORIGINAL SIZE 
    } 


//START EDITING EXPAND TO FULL WIDTH 
    - (void)textFieldDidBeginEditing:(UITextField *)textField { 
     [UIView animateWithDuration:ANIMATION_DURATION delay:ANIMATION_DELAY options:UIViewAnimationOptionCurveLinear animations:^{ 
      [_txtURLBar setFrame:CGRectMake(3, URLBAR_DEFAULT.origin.y, self.view.frame.size.width - 10, URLBAR_DEFAULT.size.height)]; 
     } completion:^(BOOL finished) { 

     }]; 

    } 

//END EDITING SHRINK BACK TO SMALL SIZE 
    - (void)textFieldDidEndEditing:(UITextField *)textField { 
     [UIView animateWithDuration:ANIMATION_DURATION delay:ANIMATION_DELAY options:UIViewAnimationOptionCurveEaseOut animations:^{ 
      [_txtURLBar setFrame:CGRectMake(URLBAR_DEFAULT.origin.x, URLBAR_DEFAULT.origin.y, URLBAR_DEFAULT.size.width, URLBAR_DEFAULT.size.height)]; 
     } completion:^(BOOL finished) { 

     }]; 
    } 

回答

0

的問題可能是由於在框架上textFieldDidBeginEditing被設置,textFieldDidEndEditing是不同的。我已經完成了與您的代碼相同的小POC,如下所示。它工作正常。

@interface ViewController() 

@property (weak, nonatomic) IBOutlet UITextField *resizeableTextField; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 


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


//START EDITING EXPAND TO FULL WIDTH 
- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    [UIView animateWithDuration:0.5 delay:0.03 options:UIViewAnimationOptionCurveLinear animations:^{ 

     self.resizeableTextField.frame = CGRectMake(CGRectGetMinX(self.resizeableTextField.frame), CGRectGetMinY(self.resizeableTextField.frame), self.resizeableTextField.frame.size.width + 40, CGRectGetHeight(self.resizeableTextField.frame)); 
    } completion:^(BOOL finished) { 

    }]; 

} 

//END EDITING SHRINK BACK TO SMALL SIZE 
- (void)textFieldDidEndEditing:(UITextField *)textField { 
    [UIView animateWithDuration:0.5 delay:0.03 options:UIViewAnimationOptionCurveEaseOut animations:^{ 
     self.resizeableTextField.frame = CGRectMake(CGRectGetMinX(self.resizeableTextField.frame), CGRectGetMinY(self.resizeableTextField.frame), self.resizeableTextField.frame.size.width - 40, CGRectGetHeight(self.resizeableTextField.frame)); 
    } completion:^(BOOL finished) { 

    }]; 
} 

@end 
相關問題