erase drawing that is saved to another image view Xcode - ios6

I am using the following code to draw on an image with UITouch. This works fine and I save the image from a temporary imageView to another image view as I go. The problem is that I want to go back in and erase some of the drawing from the saved imageView. I know that I can set the color to white and it will appear to erase it but I am drawing on top of another imageView that contains an image from my camera roll. so I need to completely clear the saved drawing.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.RealImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);
self.imageView.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(ax2*subtract),138-(ay2*subtract),740*see,850*see);
ctr = 0;
UITouch *touch = [touches anyObject];
pts[0] = [touch locationInView:self.tempImage];
lastPoint = [touch locationInView:tempImage];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self.tempImage];
currentPoint = [touch locationInView:tempImage];
ctr++;
pts[ctr] = p;
if (ctr == 4)
{
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
[self draw2];
// replace points and get ready to handle the next segment
pts[0] = pts[3];
pts[1] = pts[4];
ctr = 1;
}
NSLog(#"ctr:%d",ctr);
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
king = pencilString;
[path removeAllPoints];
ctr = 0;
UIGraphicsBeginImageContext(self.tempImage.frame.size);
self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
[self.imageView.image drawInRect:CGRectMake(0,0, self.imageView.frame.size.width, self.imageView.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
[self.tempImage.image drawInRect:CGRectMake(0,0, self.tempImage.frame.size.width, self.tempImage.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
self.tempImage.image = nil;
if (see ==2) {
self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);
self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);
self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),1480,1700);}
UIGraphicsEndImageContext();
}
- (void)draw2
{
self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
UIGraphicsBeginImageContext(self.tempImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
//here's where I try to create the erasure but all I get it a black line
if ([pencilString isEqualToString:#"clear2"]) {
mode = DrawingModePen;
if (mode == DrawingModePen) {
NSLog(#"drawing");
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [[UIColor clearColor
] CGColor]);
}
UIGraphicsBeginImageContext(self.imageView.frame.size);
[self.imageView.image drawInRect:CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
[self.imageView setAlpha:1.0];
[path stroke];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
}
else if ([pencilString isEqualToString:#"red"]) {
[[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"blue"]) {
[[UIColor colorWithRed:51.0/255.0 green:76.0/255.0 blue:255.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"yellow"]) {
[[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:0.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"orange"]) {
[[UIColor colorWithRed:255.0/255.0 green:90.0/255.0 blue:0.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"green"]) {
[[UIColor colorWithRed:0.0 green:204.0/255.0 blue:0.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"purple"]) {
[[UIColor colorWithRed:153.0/255.0 green:0.0 blue:255.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"redOrange"]) {
[[UIColor colorWithRed:255.0/255.0 green:51.0/255.0 blue:0.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"yellowOrange"]) {
[[UIColor colorWithRed:255.0/255.0 green:153.0/255.0 blue:0.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"blueViolet"]) {
[[UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:255.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"blueGreen"]) {
[[UIColor colorWithRed:0.0 green:153.0/255.0 blue:204.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"redViolet"]) {
[[UIColor colorWithRed:255.0/255.0 green:0.0 blue:153.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"yellowGreen"]) {
[[UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:0.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"white"]) {
[[UIColor whiteColor] setStroke];
}
else if ([pencilString isEqualToString:#"ltGray"]) {
[[UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"dkGray"]) {
[[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"brown"]) {
[[UIColor colorWithRed:102.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"tan"]) {
[[UIColor colorWithRed:255.0/255.0 green:204.0/255.0 blue:102.0/255.0 alpha:1.0] setStroke];
}
else if ([pencilString isEqualToString:#"black"]) {
[[UIColor blackColor] setStroke];
}
[path stroke];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
// CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
self.RealImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.imageView.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
self.tempImage.frame = CGRectMake(14-(self.zoom),138-(self.first),740*see,850*see);
UIGraphicsEndImageContext();
}

I figured this out. I placed the if statement for the "clear2" erasure at the beginning of -(void) draw2{ rewriting it by making my drawing on the imageView and having the blendMode be kCGBlendModeCopy. Then I just put everything else inside the else statement and it works.
Here is the code.
- (void)draw2
{
if ([pencilString isEqualToString:#"clear2"]) {
UIGraphicsBeginImageContext(imageView.frame.size);
[[UIColor clearColor] setStroke];
[imageView.image drawInRect:CGRectMake(0,0,imageView.frame.size.width, imageView.frame.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeCopy);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[path setLineWidth:20.0];
[path stroke];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
}
else {
UIGraphicsBeginImageContext(self.tempImage.frame.size);
[self.tempImage.image drawInRect:CGRectMake(0, 0, self.tempImage.frame.size.width, self.tempImage.frame.size.height)];
pts[3] = CGPointMake((pts[2].x + pts[4].x)/2.0, (pts[2].y + pts[4].y)/2.0); // move the endpoint to the middle of the line joining the second control point of the first Bezier segment and the first control point of the second Bezier segment
[path moveToPoint:pts[0]];
[path addCurveToPoint:pts[3] controlPoint1:pts[1] controlPoint2:pts[2]]; // add a cubic Bezier from pt[0] to pt[3], with control points pt[1] and pt[2]
if ([pencilString isEqualToString:#"red"]) {
[[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:1.0] setStroke];
[path setLineWidth:10.0*see];
}
[path stroke];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
// CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempImage setAlpha:1.0];
UIGraphicsEndImageContext();
}
}

you can call this method anywhere.
- (void)eraseLine
{
UIGraphicsBeginImageContext(self.bounds.size);
[self.viewImage drawInRect:self.bounds];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), previousPoint.x, previousPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
previousPoint = currentPoint;
[self setNeedsDisplay];
}

Related

UITableCell Width Resizing on scroll

I have an issue with a UITableViewController where as soon as you scroll down the background image of the cell increases in size. All the images are set to a width of 320px but still after you scroll the cell background image expands.
Can anyone explain why this is happening and how to fix it?
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (jsonResults.count == 0)
{
return 1;
}
else
{
return [jsonResults count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIImage *selectionBackground;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (jsonResults.count == 0)
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"topAndBottomRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
[cell.selectedBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:selectionBackground]];
cell.textLabel.text = #"Nothing was found :(";
cell.detailTextLabel.text = #"Click here to add a venue!";
//cell.textLabel.textColor= [UIColor colorWithRed:(79/255.0) green:(129/255.0) blue:(189/255.0) alpha:1] ;
cell.textLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:22];
cell.detailTextLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:14];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
cell.userInteractionEnabled = YES;
NSDictionary *venuesdict = [jsonResults objectAtIndex:indexPath.row];
NSString *addresslineone = [venuesdict objectForKey:#"address1"];
NSString *postcode = [venuesdict objectForKey:#"postcode"];
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
//rowBackground = [UIImage imageNamed:#"topAndBottomRow.png"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"topAndBottomRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
//rowBackground = [UIImage imageNamed:#"topRow.png"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"topRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
selectionBackground = [UIImage imageNamed:#"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
//rowBackground = [UIImage imageNamed:#"bottomRow.png"];
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"bottomRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
//rowBackground = [UIImage imageNamed:#"bottomRow.png"];
selectionBackground = [UIImage imageNamed:#"bottomRowSelected.png"];
}
else
{
cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"middleRow.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
//rowBackground = [UIImage imageNamed:#"middleRow.png"];
selectionBackground = [UIImage imageNamed:#"middleRowSelected.png"];
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.frame];
[imgView setImage:selectionBackground];
[cell.selectedBackgroundView addSubview:imgView];
[cell.selectedBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:selectionBackground]];
cell.textLabel.text = [venuesdict objectForKey:#"name"];
NSString *imagename = [venuesdict objectForKey:#"imagename"];
cell.imageView.image = [UIImage imageNamed:imagename];
cell.detailTextLabel.text = [NSString stringWithFormat:#" %#, %#",addresslineone,postcode];
cell.textLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:22];
cell.detailTextLabel.font = [UIFont fontWithName:#"Berlin Sans FB" size:10];
cell.backgroundColor = [UIColor clearColor];
NSString *version = [[UIDevice currentDevice] systemVersion];
BOOL isAtLeast7 = [version floatValue] >= 7.0;
if (!isAtLeast7 == YES)
{
cell.detailTextLabel.backgroundColor = [UIColor grayColor];
cell.textLabel.backgroundColor = [UIColor grayColor];
}
}
return cell;
}
Try to change this line
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.frame];
into this
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.selectedBackgroundView.bounds];

Strange AlertView Behavior

This is what is happening....
I am making a game where by the end of the challenge a customized UIAlert shows saying if the player wins or loses. The problem is... that message is displaying twice and I have no idea why....
-(void)checkOnTests{
int p1sc;
int p1t;
int p2sc;
int p2t;
// IN BETWEEN, SCORES ARE CHECKED AND THEY ARE FINE
if (p1sc > p2sc) {
NSLog(#"P1 Wins");
winnerIndex = 1; // (INT)
if (playerIndex == winnerIndex) {
self.lbl_plyer_score.textColor = [UIColor greenColor]; //WINS
self.lbl_opponent_score.textColor = [UIColor redColor]; //LOSES
[self finishChallengeWherePlayer:YES amtOfGold:2];
}else{
self.lbl_plyer_score.textColor = [UIColor redColor];
self.lbl_opponent_score.textColor = [UIColor greenColor];
[self finishChallengeWherePlayer:NO amtOfGold:0];
}
}else if (p1sc < p2sc){
winnerIndex = 2;
NSLog(#"P2 Wins");
if (playerIndex == winnerIndex) {
self.lbl_plyer_score.textColor = [UIColor greenColor];
self.lbl_opponent_score.textColor = [UIColor redColor];
[self finishChallengeWherePlayer:YES amtOfGold:2];
}else{
self.lbl_plyer_score.textColor = [UIColor redColor];
self.lbl_opponent_score.textColor = [UIColor greenColor];
[self finishChallengeWherePlayer:NO amtOfGold:0];
}
}else if (p1sc == p2sc){
NSLog(#"We got a tie match");
winnerIndex = 3;
if (((p1t == 0) && (p2t == 0)) || (p1t == p2t)) {
[self finishChallengeWherePlayer:YES amtOfGold:1];
}else if (p1t < p2t){
[self finishChallengeWherePlayer:YES amtOfGold:2];
}else if (p1t > p2t){
[self finishChallengeWherePlayer:YES amtOfGold:1];
}
}
}
The code above checks on results. So far everything is correct. No errors.
Below I implement the code where the message displays to the player telling if they win or lose the challenge.
-(void)finishChallengeWherePlayer:(BOOL)wins amtOfGold:(int)amt {
NSString *endMsg;
if (wins) {
endMsg = [NSString stringWithFormat:#"Congrats! You win the challenge, gaining: %i gold", amt];
}else{
endMsg = [NSString stringWithFormat:#"Oh No! You have lost the challenge. No gold for you"];
}
GameAlert *alert = [[GameAlert alloc] initWithTitle:#"Results" message:endMsg delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show]; // this is my custom UIAlertView that is working fine throughout the game.
[self.delegate challengeFinished:amt testname:linker];
}
The only thing that This view controller is doing differently from the rest of the game is in the box below:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[self dismissViewControllerAnimated:YES completion:nil];
}
My question is... Do I really have to tell the delegate that the challenge is over only after i dismiss this ViewController or am I doing something wrong here ? The alert is showing twice and I feel like a fool :(

drawing rounded rect on imageContext using UIBeizerpath not working

I had a requirement to draw a rounded rect while user touch is moving on iPad.
Here is my code it is showing black color rounded rect while touch moving and selected color when touch is ended. Can any one please tell me the reason?
- (void)drawRect:(CGRect)rect
{
[path setLineWidth:lineSize];
[selectedColor setStroke];
[drawImage drawInRect:rect]; // (3)
[path fill];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
startPoint=p;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
float x=startPoint.x;
float y=startPoint.y;
float z=p.x-startPoint.x;
float a=p.y-startPoint.y;
if (z<0 && a<0) {
x=p.x;
y=p.y;
z=startPoint.x-p.x;
a=startPoint.y-p.y;
}
else if (a<0)
{
x=startPoint.x;
y=startPoint.y;
z=p.x-startPoint.x;
a=startPoint.y-p.y;
}
else if(z<0)
{
x=startPoint.x;
y=startPoint.y;
z=startPoint.x-p.x;
a=p.y-startPoint.y;
}
path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(x,y,z,a) cornerRadius:10.0];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[self drawBitmap];
[self setNeedsDisplay];
}
- (void)drawBitmap // (3)
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0.0);
[selectedColor setStroke];
if (!drawImage) // first draw; paint background white by ...
{
UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
[[UIColor whiteColor] setFill];
[rectpath fill]; // filling it with white
}
[drawImage drawAtPoint:CGPointZero];
[selectedColor setFill];
[path fill];
drawImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Edit your initWithFrame method to make background of the UIView white.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
}
return self;
}

by Adding Polyline(path) on MKMapview crashing the app in ios6

I have a requirement to adding a path in my MKMapview between two annotation pin. There is no issue with ios 5 or older but when i try to run app in ios 6 app is surprisingly quit. below is my code. If there is any correction in my code then please suggest me.
1.So my question is suggest the best way how to decrees memory in my following code so that i can solve crash in my Application.
- (void)viewDidLoad
{
NVPolylineAnnotation *annotation = [[NVPolylineAnnotation alloc] initWithPoints:pathArray mapView:_mapView];
[_mapView addAnnotation:annotation];
[annotation release];
[pathArray removeAllObjects];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
NSLog(#"%s",__FUNCTION__);
if ([annotation isKindOfClass:[NVPolylineAnnotation class]])
{
NSLog(#"=========ANOTATION=========NVPolylineAnnotationView START");
//ann=[ann initWithAnnotation:annotation mapView:_mapView];
NVPolylineAnnotationView *ann=[[NVPolylineAnnotationView alloc] init];
return [[ann initWithAnnotation:annotation mapView:_mapView] autorelease];//[[[NVPolylineAnnotationView alloc] initWithAnnotation:annotation mapView:_mapView] autorelease];
}
else if([annotation isKindOfClass:[MapViewAnnotation class]])
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"pointers"] ;
// annView.rightCalloutAccessoryView = [UIButton buttonWithType: UIButtonTypeDetailDisclosure];
annView.animatesDrop=NO;
annView.canShowCallout = TRUE;
return [annView autorelease];
}
else if([annotation isKindOfClass:[PlacePin class]])
{
{
MKPinAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
static NSString *defaultPinID = #"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil )
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = NO;
}
else {
[mapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
}
return nil;
}
- (void) mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
NSLog(#"%s",__FUNCTION__);
// fixes that some marker are behind the polyline
for (int i=0; i<[views count]; i++)
{
MKAnnotationView *view = [views objectAtIndex:i];
if ([view isKindOfClass:[NVPolylineAnnotationView class]])
{
[[view superview] sendSubviewToBack:view];
/* In iOS version above 4.0 we need to update the polyline view after it
has been added to the mapview and it ready to be displayed. */
NSString *reqSysVer = #"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
[self updatePolylineAnnotationView];
}
}
}
NSLog(#"----------didAddAnnotationViews");
}
- (void)updatePolylineAnnotationView
{
NSLog(#"%s",__FUNCTION__);
MKAnnotationView *annotationView = [views objectAtIndex:0];
id <MKAnnotation> mp = [annotationView annotation];
[mapView selectAnnotation:mp animated:NO];
*/
for (NSObject *a in [_mapView annotations])
{
if ([a isKindOfClass:[NVPolylineAnnotation class]])
{
NVPolylineAnnotation *polyline = (NVPolylineAnnotation *)a;
NSObject *pv = (NSObject *)[_mapView viewForAnnotation:polyline];
if ([pv isKindOfClass:[NVPolylineAnnotationView class]])
{
NVPolylineAnnotationView *polylineView =
(NVPolylineAnnotationView *)[_mapView viewForAnnotation:polyline];
[polylineView regionChanged];
}
}
}
}

Custom swipe function in UITableViewCell doesn't work

I need to add the count in the uitableviewcell in a such a way that when I trigger the swipe function the count should be incremented in the corresponding cell and while tapping the count should be decremented.
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
NSString *CellIdentifier = #"sample";
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
UISwipeGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapFrom:)];
[self addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[self addGestureRecognizer:recognizer];
[recognizer release];
UILabel *cookieLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5, 120,30)];
cookieLabel.text = #"hello";
cookieLabel.font = [UIFont systemFontOfSize:15.0f];
cookieLabel.textColor = [UIColor blackColor];
cookieLabel.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:cookieLabel];
[cookieLabel release];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
costLabel = [[UILabel alloc] initWithFrame:CGRectMake( 200, 5, 230, 30)];
//costLabel.text = handleSwipeFrom:;
costLabel.font = [UIFont systemFontOfSize:15.0f];
costLabel.textColor = [UIColor blackColor];
costLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:costLabel];
[costLabel release];
[self setUserInteractionEnabled:YES];
return cell;
}
Don't add the UISwipeGestureRecognizer to the cell. Add it to the UITableView.
I used TISwipeableTableView as a base and modified it heavily to work correctly (they did their own touch handling, which resulted in a "weird, unnative" feeling)
- (void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [gestureRecognizer locationInView:self];
NSIndexPath *swipedIndexPath = [self indexPathForRowAtPoint:swipeLocation];
TISwipeableTableViewCell* swipedCell = (TISwipeableTableViewCell *)[self cellForRowAtIndexPath:swipedIndexPath];
if ([swipedCell isKindOfClass:[TISwipeableTableViewCell class]]) {
if (![swipedIndexPath isEqual:indexOfVisibleBackView]) {
[self hideVisibleBackView:YES];
[swipedCell revealBackView];
[self setIndexOfVisibleBackView:swipedIndexPath];
if (swipeDelegate && [swipeDelegate respondsToSelector:#selector(tableView:didSwipeCellAtIndexPath:)]){
[swipeDelegate tableView:self didSwipeCellAtIndexPath:[self indexPathForRowAtPoint:swipeLocation]];
}
}
}
}
}
}
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if ((self = [super initWithFrame:frame style:style])) {
if ([MRUserDefaults sharedMRUserDefaults].isSwipeMenuEnabled) {
UIGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)] autorelease];
[self addGestureRecognizer:swipeGesture];
}
}
return self;
}
This should get you started.
[cell addGestureRecognizer:recognizer]

Resources