iphone - UIAnimation - 从 button.frame.origin.y 显示 UIView

标签 iphone ios uianimation

我的 View 中某处有一个 UIButton。在按钮的触摸事件中,我制作了一个 UIView 出现。我使用的 UIAnimation 使 View 从窗口顶部出现。但我希望它出现在 button.frame.origin.y 中。在触摸按钮之前, View 不可见。触摸按钮时, View 应从上方位置开始出现。

代码如下:

-(IBAction) ShowInfowView:(id)sender{

    CGRect rect = viewInfo.frame;

    rect.origin.y = rect.size.height - rect.size.height+58.0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.70];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    viewInfo.frame = rect;

    [UIView commitAnimations];

}

这就是我再次隐藏 View 的方式:

-(IBAction) HideInfoView:(id)sender{

    CGRect rect = viewInfo.frame;

    rect.origin.y = -rect.size.height;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.70];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    viewInfo.frame = rect;

    [UIView commitAnimations];

}

在 viewDidLoad 中,我正在执行以下操作:

CGRect rect = viewInfo.frame;
rect.origin.y = -rect.size.height;
viewInfo.frame = rect;  

更新:

请参阅this例子。这里的 View 是从屏幕顶部出现的。我希望它从按钮 y 轴出现。为此,请考虑将按钮 y 位置向上一点。

最佳答案

所以你想要一个滑入效果,但不是从屏幕顶部,只是一些任意值?

一种方法:

1) 您应该在动画结束后创建一个具有所需 View 尺寸和位置的 View ,我们将其称为baseview

2) 将此 baseview 属性 clipsToBounds 设置为 YES。这将使 baseview 框架之外的所有 subview 不可见。

3) 添加您的动画 View 作为 baseview 的 subview ,但将框架设置为不可见(通过将其放置在 baseview):

frame = CGRectMake(0, -AnimViewHeight, AnimViewWidth, AnimViewHeight);

4) 为动画 View 框架设置动画:

//put this inside of an animation block
AnimView.frame = CGRectMake(0, 0, AnimViewWidth, AnimViewHeight);

编辑:

例子:

//define the tags so we can easily access the views later
#define BASEVIEW_TAG 100
#define INFOVIEW_TAG 101

- (void) showInfo
{
        //replace the following lines with preparing your real info view
        UIView * infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        [infoView setBackgroundColor: [UIColor yellowColor]];
        [infoView setTag: INFOVIEW_TAG];

        //this is the frame of the info view AFTER the animation finishes (again, replace with real values)
        CGRect targetframe = CGRectMake(100, 100, 100, 100);

        //create an invisible baseview
        UIView * baseview = [[UIView alloc] initWithFrame:targetframe];
        [baseview setBackgroundColor: [UIColor clearColor]];
        [baseview setClipsToBounds: YES]; //so it cuts everything outside of its bounds
        [baseview setTag: BASEVIEW_TAG];

        //add the nfoview to the baseview, and set it above the bounds frame
        [baseview addSubview: infoView];
        [infoView setFrame:CGRectMake(0, -infoView.bounds.size.height,
                                      infoView.bounds.size.width, infoView.bounds.size.height)];

        //add the baseview to the main view
        [self.view addSubview: baseview];

        //slide the view in
        [UIView animateWithDuration: 1.0 animations:^{
            [infoView setFrame: baseview.bounds];
        }];

        //if not using ARC, release the views
        [infoview release];
        [baseview release];
}

- (void) hideinfo
{
        //get the views
        UIView * baseView = [self.view viewWithTag: BASEVIEW_TAG];
        UIView * infoView = [baseView viewWithTag: INFOVIEW_TAG];

        //target frame for infoview - slide up
        CGRect out_frame = CGRectMake(0, -infoView.bounds.size.height,
                                      infoView.bounds.size.width, infoView.bounds.size.height);

        //animate slide out
        [UIView animateWithDuration:1.0
                         animations:^{
                             [infoView setFrame: out_frame];
                         } completion:^(BOOL finished) {
                             [baseView removeFromSuperview];
                         }];
    }

关于iphone - UIAnimation - 从 button.frame.origin.y 显示 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9462410/

相关文章:

ios - UIStackView:将 subview 推到边缘 ("Unlimited"间距)

ios - ImageView动画,循环遍历图片

ios - 提高旋转 UIAnimation 的速度

ios - Swift 中合并子数组

ios - NSLayoutConstraint 高度等于 Storyboard中的 origin.x

iPhone - 将按钮添加到键盘现有附件 View (来自 UIWebView 的键盘)

iphone - 更改 contentInset 后强制 UITextView 重新显示?

ios - 在 Swift 中寻找像 Uber 这样的司机动画

ios - 删除行: error 'Invalid update: invalid number of rows in section 0'

iphone - 为 APNS 创建 .pem 文件?