如何实时改变矩形的位置和大小?

发布于 2024-11-16 15:28:40 字数 722 浏览 2 评论 0原文

我想画一个矩形,并且可以实时改变矩形的位置和大小。

我尝试了以下两种方法,但视图不显示矩形。

你有什么建议或例子吗?

感谢您的帮助。

使用视图框架(仅在重新启动应用程序时显示新矩形)

UIView * drawView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
[self.view addSubview:drawView];
drawView.layer.borderColor = [[UIColor orangeColor] CGColor];
drawView.layer.borderWidth = 2;

drawView.frame = CGRectMake(r.x, r.y, r.width, r.height);

在drawRect中绘制

fav = [[FaceAugmentingView alloc] initWithFrame:[imageView frame]];
fav.backgroundColor = [UIColor clearColor];
[self.view addSubview: fav  ];
fav.face = CGRectMake(r.x, r.y, r.width, r.height);
[fav setNeedsDisplay];

I want to draw an rectangle and can change the rectangle's position and size realtime.

I have tried following two methods, but the view don't show the rectangle.

do you have any advice or example?

appreciate your help.

use view frame (it only show the new rectangle when restart the app)

UIView * drawView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];
[self.view addSubview:drawView];
drawView.layer.borderColor = [[UIColor orangeColor] CGColor];
drawView.layer.borderWidth = 2;

drawView.frame = CGRectMake(r.x, r.y, r.width, r.height);

draw in the drawRect

fav = [[FaceAugmentingView alloc] initWithFrame:[imageView frame]];
fav.backgroundColor = [UIColor clearColor];
[self.view addSubview: fav  ];
fav.face = CGRectMake(r.x, r.y, r.width, r.height);
[fav setNeedsDisplay];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2024-11-23 15:28:40

我建议您添加一种方法,可以在需要更改时重新加载面孔。声明和数组将帮助我们跟踪我们添加的所有视图。

@interface FacesViewController: UIViewController
[..]
@property (nonatomic, retain) NSMutableArray * faceViews;

- (void)reloadFaces;
[..]
@end

然后实现类似这样的 reloadFaces -

- (void)reloadFaces {
    /* remove all current views we've added */
    for ( UIView * aView in self.faceViews ) {
        [aView removeFromSuperview];
    }
    [self.faceViews removeAllObjects];

    /* Add news ones */
    int numberOfFaces = [self numberOfFacesInImage];
    for ( int i = 0; i < numberOfFaces; i++ ) {
        CGRect faceFrame = [self frameForFaceAtIndex:i];

        UIView * drawView = [[[UIView alloc] initWithFrame:faceFrame] autorelease];
        drawView.layer.borderColor = [[UIColor orangeColor] CGColor];
        drawView.layer.borderWidth = 2;
        drawView.frame = CGRectMake(r.x, r.y, r.width, r.height);

        [self.view addSubview:drawView];
        [faceViews addObject:drawView];
    }
}

我希望在发生某种更改时调用 reloadFacesnumberOfFacesInImageframeForFaceAtIndex: 是您必须根据获取数据的方式实现的方法。您还可以替换它们以适合您的数据模型。不要忘记初始化faceViews

I suggest you add a method that reloads the faces based whenever you need to alter it. Declare and array that will help us keep track of all the views we've added.

@interface FacesViewController: UIViewController
[..]
@property (nonatomic, retain) NSMutableArray * faceViews;

- (void)reloadFaces;
[..]
@end

And then implement reloadFaces something like this -

- (void)reloadFaces {
    /* remove all current views we've added */
    for ( UIView * aView in self.faceViews ) {
        [aView removeFromSuperview];
    }
    [self.faceViews removeAllObjects];

    /* Add news ones */
    int numberOfFaces = [self numberOfFacesInImage];
    for ( int i = 0; i < numberOfFaces; i++ ) {
        CGRect faceFrame = [self frameForFaceAtIndex:i];

        UIView * drawView = [[[UIView alloc] initWithFrame:faceFrame] autorelease];
        drawView.layer.borderColor = [[UIColor orangeColor] CGColor];
        drawView.layer.borderWidth = 2;
        drawView.frame = CGRectMake(r.x, r.y, r.width, r.height);

        [self.view addSubview:drawView];
        [faceViews addObject:drawView];
    }
}

I would expect reloadFaces to be called when there is a change of some kind. numberOfFacesInImage and frameForFaceAtIndex: are methods that you will have to implement based on how you get the data. You can also replace them to suit your data model. Don't forget to initialize faceViews.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文