我想在Button Click上为UITableView设置动画

时间:2017-06-23 08:39:26

标签: ios objective-c uitableview uiviewanimation

我有UITableView,我想在用户点击按钮时动画。

选择状态 - >从底部到顶部

对于未选择状态 - > TOP TO BOTTOM

为此,我尝试过以下代码。 我的TableView框架(0,39,宽度,高度)

- (IBAction)onInviteFriendsButtonClick:(UIButton *)sender {
    sender.selected =! sender.selected;

    if (sender.selected) {
        CGRect napkinBottomFrame = self.tblVWInviteFriend.frame;
        napkinBottomFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
        self.tblVWInviteFriend.frame = napkinBottomFrame;
        [UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlUp animations:^{
            self.tblVWInviteFriend.frame = CGRectMake(0, 39, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
        } completion:^(BOOL finished){/*done*/}];
    }
    else {
        CGRect napkinBottomFrame = self.tblVWInviteFriend.frame;
        napkinBottomFrame.origin.y = 39;
        self.tblVWInviteFriend.frame = napkinBottomFrame;
        [UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlDown animations:^{
            self.tblVWInviteFriend.frame = CGRectMake(0, -[UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);
        } completion:^(BOOL finished){/*done*/}];
    }
}

当我点击按钮时,它将从 BOTTOM TO TOP 中正确显示。 但当我再次点击按钮时,它将再次从 BOTTOM TO TOP

2 个答案:

答案 0 :(得分:0)

因为您已将tableview的frame.origin.y设置为-y或XY坐标。

    self.tblVWInviteFriend.frame = CGRectMake(0, -[UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);

替换为

    self.tblVWInviteFriend.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, self.tblVWInviteFriend.frame.size.width, self.tblVWInviteFriend.frame.size.height);

答案 1 :(得分:0)

试试这个:

- (IBAction)onInviteFriendsButtonClick:(UIButton *)sender {
    sender.selected =! sender.selected;

    if (sender.selected) {
        CGRect napkinBottomFrame = txtAOP.frame;
        napkinBottomFrame.origin.y = [UIScreen mainScreen].bounds.size.height;
        txtAOP.frame = napkinBottomFrame;
        [UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlUp animations:^{
            txtAOP.frame = CGRectMake(0, 39, txtAOP.frame.size.width, txtAOP.frame.size.height);
        } completion:^(BOOL finished){/*done*/}];
    }
    else {
        CGRect napkinBottomFrame = txtAOP.frame;
        napkinBottomFrame.origin.y = -[UIScreen mainScreen].bounds.size.height;
        txtAOP.frame = napkinBottomFrame;
        [UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationOptionTransitionCurlDown animations:^{
            txtAOP.frame = CGRectMake(0,39 , txtAOP.frame.size.width, txtAOP.frame.size.height);
        } completion:^(BOOL finished){/*done*/}];
    }
}