如何实时改变矩形的位置和大小?
我想画一个矩形,并且可以实时改变矩形的位置和大小。
我尝试了以下两种方法,但视图不显示矩形。
你有什么建议或例子吗?
感谢您的帮助。
使用视图框架(仅在重新启动应用程序时显示新矩形)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您添加一种方法,可以在需要更改时重新加载面孔。声明和数组将帮助我们跟踪我们添加的所有视图。
然后实现类似这样的
reloadFaces
-我希望在发生某种更改时调用
reloadFaces
。numberOfFacesInImage
和frameForFaceAtIndex:
是您必须根据获取数据的方式实现的方法。您还可以替换它们以适合您的数据模型。不要忘记初始化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.
And then implement
reloadFaces
something like this -I would expect
reloadFaces
to be called when there is a change of some kind.numberOfFacesInImage
andframeForFaceAtIndex:
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 initializefaceViews
.