Reputation: 759
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];
Upvotes: 1
Views: 1136
Reputation: 44633
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
.
Upvotes: 1