子视图不与其超级视图一起移动

时间:2015-08-12 06:24:39

标签: ios objective-c uiviewanimation subview superview

我之前已经完成了这一百万次,但现在发现自己很困惑。 当您为容器视图设置动画以更改其位置时,其子视图会随之移动吗?你不能改变子视图'您只需更改超级视图的框架,因为所有子视图都是'他们的超级视图中的位置不会改变。

但由于某些原因,这次子视图不会与他们的超级视图一起移动,它会一直停留在原来的位置。

- (void)writeCommentTapped{

    UIView *writeCommentView = [[UIView alloc]init];
    writeCommentView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview: writeCommentView];
    [self.view bringSubviewToFront:self.navView];

    self.writeCommentTextView = [[UITextView alloc]init];
    self.writeCommentTextView.backgroundColor = [UIColor blackColor];
    [writeCommentView addSubview:self.writeCommentTextView];
    [self.writeCommentTextView becomeFirstResponder];
    self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;

    writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
    self.writeCommentTextView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
    self.writeCommentTextView.center = writeCommentView.center;


    [UIView animateWithDuration:0.15
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{

                         writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);

                     } completion:^(BOOL finished){
                         if (finished) {

                         }
                     }
     ];

}

1 个答案:

答案 0 :(得分:0)

删除self.writeCommentTextView.center并设置中心

的计算框架
-(void)writeCommentTapped{


    UIView *writeCommentView = [[UIView alloc]init];
    writeCommentView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview: writeCommentView];
    [self.view bringSubviewToFront:self.navView];

    self.writeCommentTextView = [[UITextView alloc]init];
    self.writeCommentTextView.backgroundColor = [UIColor blackColor];
    [writeCommentView addSubview:self.writeCommentTextView];
    [self.writeCommentTextView becomeFirstResponder];
    self.writeCommentTextView.returnKeyType = UIReturnKeyDefault;

    writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.keyboardSize.height);
    writeCommentView.frame = CGRectMake(0, -self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height);


    self.writeCommentTextView.frame = CGRectMake(40/2, 100/2, writeCommentView.frame.size.width - 40, writeCommentView.frame.size.height - 100);
//    self.writeCommentTextView.center = writeCommentView.center;


    [UIView animateWithDuration:0.15
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{

                         writeCommentView.frame = CGRectMake(0, 0, writeCommentView.frame.size.width, writeCommentView.frame.size.height);

                     } completion:^(BOOL finished){
                         if (finished) {

                         }
                     }
     ];
}

OR

self.writeCommentTextView.center = CGPointMake(writeCommentView.frame.size.width/2.0, writeCommentView.frame.size.height/2.0);