在目标C中运行时更改矩形的大小

时间:2014-11-12 16:19:43

标签: ios objective-c

大家好我有个问题 我正在使用xcode构建应用程序,我希望在运行时设置自定义矩形的大小。

我有一个UIView类,我在其中创建自定义矩形并在ViewController的屏幕上绘制它们。问题是,每当我启动应用程序时,它只会创建所有矩形,然后显示屏幕。 我想要它做的是显示屏幕,然后动画矩形(如#34;增长动画")

这就是我在UIView类中创建矩形的方法

-(void)newRectWithX:(int) x  height:(int) height{

    _rdmRed = arc4random_uniform(100);
    _rdmRed /= 100;
    _rdmGreen = arc4random_uniform(100);
    _rdmGreen /= 100;
    _rdmBlue= arc4random_uniform(100);
    _rdmBlue /= 100;

    _color = [UIColor colorWithRed:_rdmRed green:_rdmGreen blue:_rdmBlue alpha:0.6];

    [UIView animateWithDuration:50 delay:10 options:UIViewAnimationOptionShowHideTransitionViews
                     animations:^{

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rectangle = CGRectMake(x,0,4,height);
    CGContextAddRect(context, rectangle);
    CGContextSetFillColorWithColor(context, [_color CGColor]);
    CGContextFillRect(context, rectangle);

                     }
                     completion:nil];

}

我还没有在ViewController类中做任何事情。

2 个答案:

答案 0 :(得分:0)

我有这个移动矩形,也许可以帮助你

if(destination.origin.x> 0){                 destination.origin.x = 0;                 [self.navigationController.view.superview bringSubviewToFront:self.navigationController.view];             }其他{                 destination.origin.x + = _sideMenu.view.frame.size.width;             }

        [UIView animateWithDuration:0.2 animations:^{
            self.view.frame = destination;
        }completion:^(BOOL finished) {
            if(finished){
                if (destination.origin.x > 0) {
                    [self.navigationController.view.superview bringSubviewToFront:_sideMenu.view];
                } else {
                }
            }
        }];

将destination.origin更改为destination.size,就像这样

CGRect destination = self.view.frame;

        if(destination.size.height > 10){
            destination.size.height = 10;
            [self.navigationController.view.superview bringSubviewToFront:self.navigationController.view];
        }else{
            destination.size.height += 10;
        }

        [UIView animateWithDuration:0.2 animations:^{
            self.view.frame = destination;
        }completion:^(BOOL finished) {
            if(finished){
                if (destination.size.height > 10) {
                    [self.navigationController.view.superview bringSubviewToFront:_sideMenu.view];
                } else {
                }
            }
        }];

答案 1 :(得分:0)

+ animateWithDuration:delay:options:animations:completion:

使用指定的持续时间,延迟,选项和完成处理程序为一个或多个视图设置动画。

为了能够使用该方法,您需要为视图设置动画。下面是一段代码,允许您使用该方法和CoreGraphics。

@implementation View
{
    UIView * view;
}

- (void)runAnimation {

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(setNeedsDisplay) userInfo:nil repeats:YES];

    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];


    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionShowHideTransitionViews animations:^{

        view.frame = CGRectMake(0, 0, 20, 20);

    } completion:nil];

}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rectangle = ((UIView *)[view.layer presentationLayer]).frame;
    CGContextAddRect(context, rectangle);
    CGContextSetFillColorWithColor(context, [UIColor cyanColor].CGColor);
    CGContextFillRect(context, rectangle);

}

@end