0

我有一个UIButton地方在我看来。在按钮的触摸事件中,我UIView出现了。UIAnimation我使用的使视图从窗口顶部显示。但我希望它出现在 button.frame.origin.y 中。在触摸按钮之前,视图是不可见的。触摸按钮视图应从上方位置开始出现。

这是代码:

-(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];

}

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

-(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;  

更新

请看这个例子。这里的视图是从屏幕顶部出现的。我希望它从按钮 y 轴出现。就此而言,请考虑将按钮 y 位置向上一点。

4

4 回答 4

2

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

一种方法:

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

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

3)将您的动画视图添加为baseview的子视图,但将框架设置为不可见(通过将其放在baseview上方):

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

4)动画动画视图帧:

//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];
                         }];
    }
于 2012-02-27T12:47:57.947 回答
2

我已经完成了您在我最近的项目中尝试的内容。以下步骤应该可以完成这项工作: 1. 在viewDidLoad:你的初始化中你viewToFadeIn是这样的:

viewToFadeIn = [[UIView alloc] initWithFrame:CGRectMake(20,self.view.frame.size.height+10, self.view.frame.size.widh-40, 200)]; 

//its important to initalize it outside your view
//your customization of this view
[self.view addSubview:viewToFadeIn];

2.你的ShowInfowView:方法应该是这样的:

` -(IBAction) ShowInfowView:(id)sender{

[UIView beginAnimations:@"fadeIn" context:NULL];
[UIView setAnimationDuration:0.25]; 
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, -290);
//290 is in my case, try a little for your correct value
[UIView commitAnimations];

HideInfoView 3.your:` 方法看起来像这样

-(IBAction) HideInfoView:(id)sender{
[UIView beginAnimations:@"fadeOut" context:NULL];
[UIView setAnimationDuration:0.25]; 
[UIView setAnimationDelegate:self];
viewToFadeIn.transform = CGAffineTransformMakeTranslation(0, 0);
[UIView commitAnimations];
}

编辑:4。animationFinishedMethod:

- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context{ 
   if ([animationID isEqual:@"fadeIn"]) { 
      //Do stuff 
   } 
   if ([animationID isEqual:@"fadeOut"]) { 
      //Do stuff 
   } 
}

看到这个

这应该可以解决问题。祝你好运

于 2012-02-29T18:49:39.297 回答
1

你的问题不清楚(对我来说)。这是什么?

rect.origin.y = rect.size.height - rect.size.height+58.0;

你的 UIButton 是 58 的起源吗?

你应该使用 sender.frame 等

使用块来制作这样的动画

[UIView animateWithDuration:0.5f animations:^{
    // Animation here
} completion:^(BOOL finished) {
    // onComplete
}];
于 2012-02-27T09:22:12.990 回答
0
- (void) slideIn{
    //This assumes the view and the button are in the same superview, if they're not, you'll need to convert the rects
    //I'm calling the animated view: view, and the button: button...easy enough
    view.clipToBounds = YES;
    CGRect buttonRect = button.frame;
    CGRect viewRect = CGRectMake(buttonRect.origin.x, buttonRect.origin.y + buttonRect.size.height, view.bounds.size.width, 0);
    CGFloat intendedViewHeight = view.height;
    view.frame = viewRect;
    viewRect.size.height = intendedViewHeight;
   [UIView animateWithDuration:.75 delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
        view.frame = viewRect;
    }completion:nil];
}

这将导致视图看起来从按钮下方滑出。要将视图滑回,只需反转动画即可。如果您希望从按钮向上滑动它,您需要确保您的起始 'y' 是按钮的 'y' 值(而不是 'y' + 'height')并为高度和需要动画的视图的 y 值。

于 2012-02-29T18:31:48.530 回答