Hello everybody,
I'm new in this forum and i'm french (sorry if my english is not very well)
Here is my first post, i'm beginner on programming in objective C and i have some problems.
I'm programming a mini application that display random points in a custom view.
The user enter a number of point in a UITexfield and then he clicks on a button that execute a function which display random points in the view.
But the points don't appear in the view despite i use the function setNeedDisplay :
ViewController.h
ViewController.m
maVue.h
maVue.m
Anybody have a solution ?
Thanks
I'm new in this forum and i'm french (sorry if my english is not very well)
Here is my first post, i'm beginner on programming in objective C and i have some problems.
I'm programming a mini application that display random points in a custom view.
The user enter a number of point in a UITexfield and then he clicks on a button that execute a function which display random points in the view.
But the points don't appear in the view despite i use the function setNeedDisplay :
ViewController.h
Code:
#import <UIKit/UIKit.h>
#import "maVue.h"
@interface ViewController : UIViewController{
IBOutlet UITextField *ptsNumber;
IBOutlet UILabel *piResult;
IBOutlet maVue *contentView;
int contentViewWidth;
int contentViewHeight;
}
- (IBAction)calculPI:(id)sender;
@end
ViewController.m
Code:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (id) initWithCoder:(NSCoder *)aDecoder {
NSLog(@"initWithCoder");
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (float)generateRandomNumberBetweenMin:(int)min Max:(int)max{
return (float) ((arc4random() % (max-min+1)) + min);
}
- (IBAction)calculPI:(id)sender {
CGRect viewBounds = [contentView bounds];
contentViewWidth = (int) viewBounds.size.width;
contentViewHeight = (int) viewBounds.size.height;
float ptsNumber = [ptsNumber.text floatValue];
float ptsInTheCircle = 0;
float randX = 0;
float randY = 0;
CGPoint point;
for(int i=0; i<ptsNumber; i++){
randX = [self generateRandomNumberBetweenMin:0 Max:contentViewWidth];
randY = [self generateRandomNumberBetweenMin:0 Max:contentViewHeight];
point.x = randX;
point.y = randY;
NSValue *ptsValueStore = [NSValue valueWithCGPoint:point];
[contentView addPoint:ptsValueStore];
printf("view bounds %d + %d\n", contentViewWidth, contentViewHeight);
printf("%f + %f\n", point.x, point.y);
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
@end
maVue.h
Code:
#import <UIKit/UIKit.h>
@interface maVue : UIView
@property (nonatomic, strong) NSMutableArray *pts;
-(void) addPoint:(NSValue *)point;
@end
maVue.m
Code:
#import "maVue.h"
@implementation maVue
@synthesize pts;
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
//Initialisation du centre du repère
CGPoint center;
center.x = bounds.origin.x;
center.y = bounds.origin.y;
//Initialisation du rayon + traçé de l'arc
float maxRadius = bounds.size.width;
CGContextSetLineWidth(ctx, 1);
CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
CGContextAddArc(ctx, center.x, center.y, maxRadius, 0.0, 2 * M_PI, YES);
CGContextStrokePath(ctx);
printf("my view bounds : %f\n", maxRadius);
printf("nbpoint : %d\n",(int) [self.pts count]);
CGPoint pt;
for(NSValue *valuePt in self.pts){
pt = [valuePt CGPointValue];
//Ajout des points sur le cercle
CGRect borderRect = CGRectMake(pt.x, pt.y, 1.0, 1.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
CGContextFillEllipseInRect (context, borderRect);
CGContextFillPath(context);
}
}
-(void) addPoint:(NSValue *)point {
[self.pts addObject:point];
printf("Add nbpoint : %D\n",(int) [self.pts count]);
[self setNeedsDisplay];
}
@end
Anybody have a solution ?
Thanks