UIView通过动画分为两部分

时间:2012-01-09 12:18:59

标签: ios

我正在与UIView合作。我想在那个视图上做动画。点击视图时,应将其分为两部分并向两侧移动。请帮助我找到合适的解决方案。

1 个答案:

答案 0 :(得分:1)

@implementation SplitView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code
        self.backgroundColor = [UIColor blueColor];

        UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(split)];
        [self addGestureRecognizer:ges];
        [ges release];
    }
    return self;
}
- (void)split {
    CGRect f = self.frame;
    CGRect f1 = CGRectMake(CGRectGetMinX(f), f.origin.y, f.size.width/2, f.size.height);
    CGRect f2 = CGRectMake(CGRectGetMidX(f), f.origin.y, f.size.width/2, f.size.height);

    SplitView *view1 = [[[SplitView alloc] initWithFrame:f1] autorelease];
    [self.superview addSubview:view1];

    SplitView *view2 = [[[SplitView alloc] initWithFrame:f2] autorelease];
    [self.superview addSubview:view2];

    f1.origin.x -= 30;
    f2.origin.x += 30;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    view1.frame = f1;
    view2.frame = f2;
    [UIView commitAnimations];

    [self removeFromSuperview];
}

@end

尝试创建这样的UIView类。