iphone弹出窗口效果的制作(Core animation, CALayer)

来源:互联网 发布:淘宝网不能加入购物车 编辑:程序博客网 时间:2024/06/01 07:25

效果类似人人网微薄客户端的弹出效果

staticCGFloat kTransitionDuration =0.3;

- (void)initView

{

   UIWindow *window = [UIApplicationsharedApplication].keyWindow;

    if (!window)

    {

        window = [[UIApplicationsharedApplication].windowsobjectAtIndex:0];

    }

   

    _backgroundView = [[UIViewalloc]initWithFrame:window.bounds];

   

   // 这个可以使背景变成灰色,类似UIAlertView弹出的效果

  _backgroundView.backgroundColor =  [[UIColorblackColor]colorWithAlphaComponent:0.35];


//叠加到window,这样他的父窗口就无法再响应点击消息了.

    [windowaddSubview:_backgroundView];

   

    self.frame =CGRectMake(10,60,300,380);

    [_backgroundViewaddSubview:self];

   self.backgroundColor = [UIColor orangeColor];

   

   //一系列动画效果,先放大0.1,在缩小0.1,随后还原原始大小,达到反弹效果

   self.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.05,0.05);

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:kTransitionDuration/1.5];

    [UIViewsetAnimationDelegate:self];

    [UIViewsetAnimationDidStopSelector:@selector(bounceAnimationStopped)];

   self.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.1,1.1);

    [UIViewcommitAnimations];

}


- (void)bounceAnimationStopped {

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:kTransitionDuration/2];

    [UIViewsetAnimationDelegate:self];

    [UIViewsetAnimationDidStopSelector:@selector(bounce2AnimationStopped)];

   self.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.9,0.9);

    [UIViewcommitAnimations];

}


- (void)bounce2AnimationStopped {

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:kTransitionDuration/2];

   self.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    [UIViewcommitAnimations];

}


如果想实现圆角的视图:

    CALayer *subLayer = [CALayerlayer];

    subLayer.backgroundColor = [UIColorwhiteColor].CGColor;

    subLayer.shadowOffset =CGSizeMake(0,10);

    subLayer.shadowRadius =5.0;

    subLayer.shadowColor = [UIColorblackColor].CGColor;

    subLayer.shadowOpacity =0.8;

    subLayer.frame =CGRectMake(30,30,150,190);

    subLayer.cornerRadius =10;

    subLayer.borderColor =  [[UIColorblackColor]colorWithAlphaComponent:0.75].CGColor;

    subLayer.borderWidth =4;

    [self.layeraddSublayer:subLayer];


// 如果在层上添加的视图如图片比父视图大,应该试用maskToBounds = YES;