I have an imageview to which I have added certain custom subview as dots. There is also an NSMutableArray where I add the corresponding CGPoint. I also add set the same CGPoint as property in the custom dot subview.
When I try to delete a random point out of the total in the Array, I delete it both from array and remove the corresponding subview from ImageView if its datapoint property matches with the same one which was in the array on that index.
This is my code in the touchesStarted of the custom ImageView to add the datapoint. There is also some extra code to keep the latest added point of yellow color and old ones to be red.
SBPointView *pointView = [[SBPointView alloc] initWithFrame:CGRectMake(touchPoint.x - kPointViewSize/2, touchPoint.y - kPointViewSize, 5, 5)];
pointView.backgroundColor = [UIColor yellowColor];
pointView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
CGSize imageSize = self.image.size;
CGRect frame = self.frame;
CGAffineTransform transform = self.transform;
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(imageSize.width/frame.size.width, imageSize.height/frame.size.height);
transform = CGAffineTransformConcat(transform, scaleTransform);
CGPoint dataPoint = CGPointApplyAffineTransform(touchPoint, transform);//[mOfflineView convertPoint:touchPoint fromView:self];//[self convertPoint:touchPoint toView:mOfflineView];
NSLog(@"**** Datapoint = %@", NSStringFromCGPoint(dataPoint));
pointView.dataPoint = dataPoint;
[mDataPoints addObject:NSStringFromCGPoint(dataPoint)];
for (SBPointView *tempPointView in self.subviews) {
if ([tempPointView isKindOfClass:[SBPointView class]]) {
tempPointView.backgroundColor = [UIColor redColor];
}
}
[self addSubview:pointView];
[pointView release];
And Here is my code to delete a random PointView,
-(void)deleteDatapointAtIndex:(NSUInteger)index{
if (index < [mDataPoints count]) {
CGPoint dataPoint = CGPointFromString([mDataPoints objectAtIndex:index]);
for (SBPointView *subview in self.subviews) {
if ([subview isKindOfClass:[SBPointView class]]) {
if (subview.dataPoint.x == dataPoint.x && subview.dataPoint.y == dataPoint.y) {
[mDataPoints removeObjectAtIndex:index];
[subview removeFromSuperview];
break;
}
}
}
}
}
But for some reason I don't know why, I can't delete certain datapoints from the array and also the remove the subviews and the if condition fails, am I doing something wrong here, which I cant see.
UPDATE : I have tried to put an NSLog in the for loop to check what all datapoints i am checking against and what datapoint I have selected from the index, turns out the that in the cases where If fails I have a point like this, {483.2, 601.6} and these are the points left {483.2, 601.6}, {483.2, 681.6}.
Its quite clear that my CGPoint is same as the first one {483.2, 601.6}
and in the if condition I am checking for equality f both x and y of both CGPoints but the if condition is still failing ! is it something to do with precision loss when converting CGPoint to string and back ?