开发者

Animating Sibling Views

I have a grid of UIViews tha开发者_如何学运维t each have a touch event attached. When a UIView is touched I would like to fade all of its siblings.

Does anyone have any direction to give on this? Can the fading siblings be handled by the UIView that was touched or should the view controller fade the siblings?

EDIT: Figured it out:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UIView *subview in [self.superview subviews]) {
        if ( subview != self ) {
            subview.layer.opacity = 0.5;
        }      
    }

    [super bringSubviewToFront:self];

}


You can also do it at the UIView level. Just wrap your change to the view's alpha inside a UIView animation block. Like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  [UIView beginAnimations:nil context:NULL];  
  [UIView setAnimationDuration:0.5];
  for (UIView *subview in [self.superview subviews]) {
    if ( subview != self ) {
      [subview setAlpha:0.5];
    }      
  }
  [UIView commitAnimations];    
  [super bringSubviewToFront:self];

}

This should fade all subviews to half opacity over a half second. You got it sorted, but I just thought I would throw in the UIView way of achieving the same result.

Best regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜