MapKit: How can I pan and zoom to fit all my annotations? - iphone

So I've got a bunch of annotations spread out on my map... everything is just dandy. Now I need to be able to set the map's position and zoom so they all fit perfectly. How can I do this?

From Apple forums...
- (void)recenterMap {
NSArray *coordinates = [self.mapView valueForKeyPath:#"annotations.coordinate"];
CLLocationCoordinate2D maxCoord = {-90.0f, -180.0f};
CLLocationCoordinate2D minCoord = {90.0f, 180.0f};
for(NSValue *value in coordinates) {
CLLocationCoordinate2D coord = {0.0f, 0.0f};
[value getValue:&coord];
if(coord.longitude > maxCoord.longitude) {
maxCoord.longitude = coord.longitude;
}
if(coord.latitude > maxCoord.latitude) {
maxCoord.latitude = coord.latitude;
}
if(coord.longitude < minCoord.longitude) {
minCoord.longitude = coord.longitude;
}
if(coord.latitude < minCoord.latitude) {
minCoord.latitude = coord.latitude;
}
}
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;
region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;
[self.mapView setRegion:region animated:YES];
}

#interface MKMapView (zoomToFit)
- (void)zoomToFitOverlays; //Animation defaults to YES
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay;
- (void)zoomToFitOverlays:(NSArray *)someOverlays;
- (void)zoomToFitOverlaysAnimated:(BOOL)animated;
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay animated:(BOOL)animated;
- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated;
- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated insetProportion:(CGFloat)insetProportion; //inset 0->1, defaults in other methods to .1 (10%)
#end
#implementation MKMapView (zoomToFit)
#pragma mark -
#pragma mark Zoom to Fit
- (void)zoomToFitOverlays {
[self zoomToFitOverlaysAnimated:YES];
}
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay {
[self zoomToFitOverlay:[NSArray arrayWithObject:anOverlay] animated:YES];
}
- (void)zoomToFitOverlays:(NSArray *)someOverlays {
[self zoomToFitOverlays:someOverlays animated:YES];
}
- (void)zoomToFitOverlaysAnimated:(BOOL)animated {
[self zoomToFitOverlays:self.overlays animated:animated];
}
- (void)zoomToFitOverlay:(id<MKOverlay>)anOverlay animated:(BOOL)animated {
[self zoomToFitOverlays:[NSArray arrayWithObject:anOverlay] animated:YES];
}
- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated {
[self zoomToFitOverlays:someOverlays animated:animated insetProportion:.1];
}
- (void)zoomToFitOverlays:(NSArray *)someOverlays animated:(BOOL)animated insetProportion:(CGFloat)insetProportion {
//Check
if ( !someOverlays || !someOverlays.count ) {
return;
}
//Union
MKMapRect mapRect = MKMapRectNull;
if ( someOverlays.count == 1 ) {
mapRect = ((id<MKOverlay>)someOverlays.lastObject).boundingMapRect;
} else {
for ( id<MKOverlay> anOverlay in someOverlays ) {
mapRect = MKMapRectUnion(mapRect, anOverlay.boundingMapRect);
}
}
//Inset
CGFloat inset = (CGFloat)(mapRect.size.width*insetProportion);
mapRect = [self mapRectThatFits:MKMapRectInset(mapRect, inset, inset)];
//Set
MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);
[self setRegion:region animated:animated];
}
#end

Related

Set the correct zoom in a MKMapView

I made a count-steps app, I count the steps with the iPhone accelerometer and now I want to display the path I made on a mapView. I can easily show the map and catch the right position with the iPhone GPS, I can display the start and the end point and I can to draw a polyline to show the path I made. Now my problem is how to set a correct zoom on the map? When I open the ViewController with map the zoom it's too far and I've to zoom to see the path I made.
So my question is how to set a correct zoom?
I post here the code of the View Controller with the mapView:
ResultViewController.m
#import "ResultViewController.h"
#import "MapAnnotation.h"
#interface ResultViewController ()
#property (nonatomic, strong)NSMutableArray *mapAnnotation;
#property (nonatomic) BOOL needUpdateRegion;
#end
#implementation ResultViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.labelResult.text = self.totalSteps;
[self.mapView setDelegate:self];
self.needUpdateRegion = YES;
self.mapAnnotation = [[NSMutableArray alloc]initWithCapacity:2];
CLLocation *startLocation = [self.locationArray objectAtIndex:0];
CLLocation *stopLocation = [self.locationArray lastObject];
NSString *startLatitude = [NSString stringWithFormat:#"%f", startLocation.coordinate.latitude];
NSString *startLongitude = [NSString stringWithFormat:#"%f", startLocation.coordinate.longitude];
NSString *stopLatitude = [NSString stringWithFormat:#"%f", stopLocation.coordinate.latitude];
NSString *stopLongitude = [NSString stringWithFormat:#"%f", stopLocation.coordinate.longitude];
MapAnnotation *startPlace = [[MapAnnotation alloc]initWithCoordinate:startLatitude andLong:startLongitude];
MapAnnotation *stopPlace = [[MapAnnotation alloc]initWithCoordinate:stopLatitude andLong:stopLongitude];
[self.mapAnnotation insertObject:startPlace atIndex:0];
[self.mapAnnotation insertObject:stopPlace atIndex:1];
if (self.mapAnnotation) {
[self.mapView removeAnnotations:self.mapView.annotations];
}
[self.mapView addAnnotation:self.mapAnnotation[0]];
[self.mapView addAnnotation:self.mapAnnotation[1]];
[self updateRegion];
[self createMKpolylineAnnotation];
}
- (void)updateRegion
{
self.needUpdateRegion = NO;
CGRect boundingRect;
BOOL started = NO;
for (id <MKAnnotation> annotation in self.mapView.annotations) {
CGRect annotationRect = CGRectMake(annotation.coordinate.latitude, annotation.coordinate.longitude, 0, 0);
if (!started) {
started = YES;
boundingRect = annotationRect;
} else {
boundingRect = CGRectUnion(boundingRect, annotationRect);
}
}
if (started) {
boundingRect = CGRectInset(boundingRect, -0.2, -0.2);
if ((boundingRect.size.width < 20) && (boundingRect.size.height < 20)) {
MKCoordinateRegion region;
region.center.latitude = boundingRect.origin.x + boundingRect.size.width / 2;
region.center.longitude = boundingRect.origin.y + boundingRect.size.height / 2;
region.span.latitudeDelta = boundingRect.size.width;
region.span.longitudeDelta = boundingRect.size.height;
[self.mapView setRegion:region animated:YES];
}
}
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor blueColor];
polylineView.lineWidth = 5.0;
return polylineView;
}
- (void)createMKpolylineAnnotation {
NSInteger numberOfSteps = self.locationArray.count;
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * numberOfSteps);
for (NSInteger index = 0; index < numberOfSteps; index++) {
CLLocation *location = [self.locationArray objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coords[index] = coordinate;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coords count:numberOfSteps];
[self.mapView addOverlay:polyLine];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)restart:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
What's wrong with this code? There's someone that can help me to zoom the map correctly?
Thank you
In that case I would recommend you to call the following method in order to zoom to a region of the map, you could add the call at the end your method - (void)createMKpolylineAnnotation;
Call:
[self zoomToPolyLine: self.mapView polyLine:polyLine animated:YES];
Method:
-(void)zoomToPolyLine:(MKMapView*)map polyLine:(MKPolyline*)polyLine
animated:(BOOL)animated
{
MKPolygon* polygon = [MKPolygon polygonWithPoints:polyLine.points count:polyLine.pointCount];
[map setRegion:MKCoordinateRegionForMapRect([polygon boundingMapRect])
animated:animated];
}
firstly add this line in view did load after you set the mapview delegate as
[self.mapView setDelegate:self];
self.needUpdateRegion = YES;
mapView.zoomEnabled = YES;
mapView.scrollEnabled = YES;
....
}
then modify the below line to set the zoom level as required by you 60000=60 km..
[mapView setRegion: MKCoordinateRegionMakeWithDistance(site.coordinate, 60000, 60000) animated:YES];
or you can try this method for setting scale of zoom as
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGImageRef imageReference = self.overlayImage.CGImage;
MKMapRect theMapRect = self.overlay.boundingMapRect;
CGRect theRect = [self rectForMapRect:theMapRect];
CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);
}
hope this helps you..Cheers!!

How to set map rect with user location at the centre?

I have a mapView with one pin and the users location. I have made it so the VisibleMapRect is set so both pins can be seen.
The problem Im having is that I am using MKUserTrackingModeFollowWithHeading to show the users move movements and when I set the rect like this the movement is very jerky. I believe this is because the user location pin is not in the centre of the map.
How can I get both pins to be visible but keep the user location in the centre?
This is my code that starts the location and sets the rect.
[locationManager startUpdatingLocation];
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
locationManager.headingFilter = kCLHeadingFilterNone;
[locationManager startUpdatingHeading];
//set the view to fit both the pins
MKMapPoint annotationPoint = MKMapPointForCoordinate(MapView.userLocation.coordinate);
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
for (id <MKAnnotation> annotation in MapView.annotations)
{
if (![annotation isKindOfClass:[MKUserLocation class]] ) {
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
NSLog(#"%f",zoomRect.size.height);
NSLog(#"%f",zoomRect.size.width);
[MapView setVisibleMapRect:zoomRect animated:NO];
}
I have tried commenting out the part that sets the rect and the movement of the user is smooth so I can tell my problem is this code.
I have also tried setting the centre point after the rect using
[MapView setCenterCoordinate:MapView.userLocation.coordinate animated:YES];
This did not seem to work. Also setting this after may make it so the other pin is not shown anymore.
I use this method:
- (void)setRegionFromCoordinates:(NSArray *)waypoints animated:(BOOL)animated {
if (waypoints != nil) {
CLLocationDegrees maxX = -DBL_MAX;
CLLocationDegrees maxY = -DBL_MAX;
CLLocationDegrees minX = DBL_MAX;
CLLocationDegrees minY = DBL_MAX;
for (NSUInteger i=0; i < waypoints.count; i++) {
CLLocationCoordinate2D currentLocation = [waypoints objectAtIndex:i];
MKMapPoint mapPoint = MKMapPointForCoordinate(currentLocation);
if (mapPoint.x > maxX) {
maxX = mapPoint.x;
}
if (mapPoint.x < minX) {
minX = mapPoint.x;
}
if (mapPoint.y > maxY) {
maxY = mapPoint.y;
}
if (mapPoint.y < minY) {
minY = mapPoint.y;
}
}
if (maxX != -DBL_MAX && minX != DBL_MAX) {
MKMapRect mapRect = MKMapRectMake(minX,minY,maxX-minX,maxY-minY);
[map setVisibleMapRect:mapRect edgePadding:UIEdgeInsetsMake(50.f, 50.f, 50.f, 50.f) animated:animated];
}
}
}
For Display All the Annotation with Your Current location use following method
- (void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if ([mapView.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(id<MKAnnotation> annotation in mapView.annotations) {
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}

Using Existing Segmented Control to Control mkmapkit zoom

I have a segmented control which controls the address and phone number as well as some other labels on a screen.
I am trying to also get this same control to zoom into the specific location of three locations on the mkmapview and make the zoom level at street view depending what button they push. Any help would be appreciated.
Here is my .m file:
#implementation LocationInfoViewController;
#synthesize mapView;
-(void)viewDidLoad
{
[super viewDidLoad];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setDelegate:self];
MKCoordinateRegion morristown = { {0.0, 0.0} , {0.0, 0.0} };
morristown.center.latitude = 40.797011;
morristown.center.longitude = -74.482855;
morristown.span.longitudeDelta = 0.60f;
morristown.span.latitudeDelta = 0.60f;
[mapView setRegion:morristown animated:YES];
Annotation *ann1 = [[Annotation alloc] init];
ann1.title = #"Sushi Lounge";
ann1.subtitle = #"Morristown";
ann1.coordinate = morristown.center;
[mapView addAnnotation: ann1];
MKCoordinateRegion hoboken = { {0.0, 0.0} , {0.0, 0.0} };
hoboken.center.latitude = 40.738496;
hoboken.center.longitude = -74.029434;
hoboken.span.longitudeDelta = 0.60f;
hoboken.span.latitudeDelta = 0.60f;
[mapView setRegion:hoboken animated:YES];
Annotation *ann2 = [[Annotation alloc] init];
ann2.title = #"Sushi Lounge";
ann2.subtitle = #"Hoboken";
ann2.coordinate = hoboken.center;
[mapView addAnnotation:ann2];
MKCoordinateRegion totawa = { {0.0, 0.0} , {0.0, 0.0} };
totawa.center.latitude = 40.875682;
totawa.center.longitude = -74.301787;
totawa.span.longitudeDelta = 0.60f;
totawa.span.latitudeDelta = 0.60f;
[mapView setRegion:hoboken animated:YES];
Annotation *ann3 = [[Annotation alloc] init];
ann3.title = #"Sushi Lounge";
ann3.subtitle = #"Totawa";
ann3.coordinate = totawa.center;
[mapView addAnnotation:ann3];
}
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"current"];
MyPin.pinColor = MKPinAnnotationColorPurple;
UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[advertButton addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
MyPin.rightCalloutAccessoryView = advertButton;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop=TRUE;
MyPin.canShowCallout = YES;
return MyPin;
}
-(void)button:(id)sender {
NSLog(#"Button action");
}
//Start of Segment control
-(IBAction) changeSegment:(id)sender
{
//streetLabel---------------------------------------------
if(control.selectedSegmentIndex == 0) {
streetLabel.text = #"12 Schuyler Place";
}
if(control.selectedSegmentIndex == 1) {
streetLabel.text = #"200 Hudson St.";
}
if(control.selectedSegmentIndex == 2) {
streetLabel.text = #"235 Route 46 West";
}
//phoneLabel---------------------------------------------
if(control.selectedSegmentIndex == 0) {
phoneLabel.text = #"(973) 539-1135";
}
if(control.selectedSegmentIndex == 1) {
phoneLabel.text = #"(201) 386-1117";
}
if(control.selectedSegmentIndex == 2) {
phoneLabel.text = #"(973) 890-0007";
}
//Monday----------------------------------------------
if(control.selectedSegmentIndex == 0) {
hoursLabel1.text = #"11:30am - 11:00pm";
}
if(control.selectedSegmentIndex == 1) {
hoursLabel1.text = #"11:30pm - 11:30pm";
}
if(control.selectedSegmentIndex == 2) {
hoursLabel1.text = #"11:30am - 10:30pm";
}
//Tuesday------------------------------------------
if(control.selectedSegmentIndex == 0) {
hoursLabel2.text = #"11:30am - 11:00pm";
}
if(control.selectedSegmentIndex == 1) {
hoursLabel2.text = #"11:30am - 11:30pm";
}
if(control.selectedSegmentIndex == 2) {
hoursLabel2.text = #"11:30am - 10:30pm";
}
//Wednesday------------------------------------------
if(control.selectedSegmentIndex == 0) {
hoursLabel3.text = #"11:30am - 11:30pm";
}
if(control.selectedSegmentIndex == 1) {
hoursLabel3.text = #"11:30am - 11:30pm";
}
if(control.selectedSegmentIndex == 2) {
hoursLabel3.text = #"11:30am - 12:00am";
}
//Thursday------------------------------------------
if(control.selectedSegmentIndex == 0) {
hoursLabel4.text = #"11:30am - 12:00am";
}
if(control.selectedSegmentIndex == 1) {
hoursLabel4.text = #"11:30am - 12:00pm";
}
if(control.selectedSegmentIndex == 2) {
hoursLabel4.text = #"11:30am - 11:30pm";
}
//Friday------------------------------------------
if(control.selectedSegmentIndex == 0) {
hoursLabel5.text = #"11:30am - 12:00pm";
}
if(control.selectedSegmentIndex == 1) {
hoursLabel5.text = #"11:30am - 1:00am";
}
if(control.selectedSegmentIndex == 2) {
hoursLabel5.text = #"11:30am - 12:00am";
}
//Saturday------------------------------------------
if(control.selectedSegmentIndex == 0) {
hoursLabel6.text = #"12:00pm - 12:00am";
}
if(control.selectedSegmentIndex == 1) {
hoursLabel6.text = #"12:00pm - 1:00am";
}
if(control.selectedSegmentIndex == 2) {
hoursLabel6.text = #"12:00pm - 12:00am";
}
//Sunday------------------------------------------
if(control.selectedSegmentIndex == 0) {
hoursLabel7.text = #"12:00pm - 11:00pm";
}
if(control.selectedSegmentIndex == 1) {
hoursLabel7.text = #"12:00pm - 11:30pm";
}
if(control.selectedSegmentIndex == 2) {
hoursLabel7.text = #"12:00pm - 10:30pm";
}
if (control.selectedSegmentIndex == 0) {
mapView.center.x == 40.738496;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
better just do more reading, here is a sample code you might want to study:
MapCallouts
One simple solution is to make the annotation variables ann1, ann2, and ann3 ivars instead of local variables in viewDidLoad.
You will then be able to easily access them and change the map's coordinates in the changeSegment: method:
if (control.selectedSegmentIndex == 0) {
//set streetLabel, phoneLabel, and hoursLabelX here also
[mapView setRegion:MKCoordinateRegionMakeWithDistance(ann1.coordinate, 1000, 1000) animated:YES];
}
else
if (control.selectedSegmentIndex == 1) {
//set streetLabel, phoneLabel, and hoursLabelX here also
[mapView setRegion:MKCoordinateRegionMakeWithDistance(ann2.coordinate, 1000, 1000) animated:YES];
}
else
if (control.selectedSegmentIndex == 2) {
//set streetLabel, phoneLabel, and hoursLabelX here also
[mapView setRegion:MKCoordinateRegionMakeWithDistance(ann3.coordinate, 1000, 1000) animated:YES];
}
By the way, you can greatly simplify and shorten the code in changeSegment: by putting all the statements that apply to the same condition together as mentioned in the comments above instead of separately.

Zoom issue with UIImageView

I am trying to get pinch/zoom work but to no luck... please check code attached any help is appreciated.
#import "SlideShowViewController.h"
#import "SomAppDelegate.h"
#import "MainCategory.h"
#import <sqlite3.h>
#import "UIImage+Resizing.h"
NSInteger Val;
NSInteger AIX;
NSInteger AIY;
NSInteger Height;
NSInteger Pre;
NSInteger Valtab;
NSInteger ImageVal;
NSInteger ValScroll;
UIToolbar *tabBar;
UIActivityIndicatorView *activityIndicator;
UIActivityIndicatorView *activityIndicator1;
#class SomAppDelegate;
#class SlideShowViewController;
#interface SlideShowView : UIView<UIScrollViewDelegate>
{
NSArray * mImages;
UIImageView * mLeftImageView;
UIImageView * mCurrentImageView;
UIImageView * mRightImageView;
NSUInteger mCurrentImage;
BOOL mSwiping;
UIImageView * result;
NSString *imageName;
CGFloat mSwipeStart;
UIScrollView *NewView;
NSTimer *timer;
//////////////////////////////////////////////////////for pinch and Zoom//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CGPoint gestureStartPoint;
CGPoint wmsImageViewCenter;
double unitDistance;
double initialDistance;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
#property (nonatomic, retain)UIView *NewView;
- (double)getDistance:(CGPoint)fromPoint toPoint:(CGPoint)otherPoint;
-(id)initWithImages:(NSArray *)inImages;
#end
#interface SlideShowView(PrivateMethods)
-(UIImageView *)newPieceViewWithImageNamed:(NSString *)imageName atPostion:(CGPoint)centerPoint;
- (void)animateFirstTouch:(double)scale;
#end
#pragma mark -
#implementation SlideShowView
#synthesize NewView;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
if (inImageIndex >= [mImages count])
{
return nil;
}
UIImage *pigImage1 =[UIImage imageWithContentsOfFile:[mImages objectAtIndex:inImageIndex]];
result=[[UIImageView alloc] initWithImage:pigImage1];
pigImage1=nil;
[pigImage1 release];
result.opaque = YES;
CGRect frameRect = [result frame];
unitDistance = [self getDistance:CGPointMake(0, 0) toPoint:CGPointMake(frameRect.size.width, frameRect.size.height)] / 4.0;
result.userInteractionEnabled =NO;
result.alpha = 1.0;
result.backgroundColor = [UIColor blackColor];
result.contentMode = UIViewContentModeScaleAspectFit;
result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
return result;
}
-(UIImage*)createImage:(NSUInteger)inImageIndex
{
if (inImageIndex >= [mImages count])
{
return nil;
}
UIImage *pigImage1 =[UIImage imageWithContentsOfFile:[mImages objectAtIndex:inImageIndex]];
return pigImage1;
}
- (id)initWithImages:(NSArray *)inImages
{
initialDistance = 1;
unitDistance = 1;
[self setMultipleTouchEnabled:YES];
self.multipleTouchEnabled = YES;
if (self = [super initWithFrame:CGRectZero])
{
mImages = [inImages retain];
NSUInteger imageCount=[inImages count];
ImageVal=imageCount;
if (imageCount > 0)
{
mCurrentImageView = [self createImageView:0];
[self addSubview:mCurrentImageView];
result=nil;
if (imageCount > 1)
{
mRightImageView = [self createImageView:1];
[self addSubview:mRightImageView];
result=nil;
}
}
self.opaque = YES;
self.backgroundColor = [UIColor blackColor];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
return self;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc
{
[mImages release];
[result release];
result.image=nil;
result=nil;
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)layoutSubviews
{
if (mSwiping)
return;
CGSize contentSize =self.frame.size; //self.frame.size;
mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//////////////////////////////////////////// code by pinch and zoom ////////////////////////////////////////////////////////////////
if (mCurrentImageView != nil)
{
if ([touches count] > 1) {
NSArray *touchArray = [touches allObjects];
CGPoint p1 = [[touchArray objectAtIndex:0] locationInView:self];
CGPoint p2 = [[touchArray objectAtIndex:1] locationInView:self];
initialDistance = [self getDistance:p1 toPoint:p2];
}
else
{
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self];
wmsImageViewCenter = mCurrentImageView.center;
}
}
//////////////////////////////////////// code by rajeev ////////////////////////////////////////////////////////////////
if ([touches count] != 1)
return;
mSwipeStart = [[touches anyObject] locationInView:self].x;
mSwiping = YES;
mLeftImageView.hidden = NO;
mCurrentImageView.hidden = NO;
mRightImageView.hidden = NO;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGSize contentSize =self.frame.size;
///////////////////////////////////////////////////for pinch and zoom/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (mCurrentImageView != nil)
{
if ([touches count] > 1)
{
NSArray *touchArray = [touches allObjects];
CGPoint p1 = [[touchArray objectAtIndex:0] locationInView:self];
CGPoint p2 = [[touchArray objectAtIndex:1] locationInView:self];
float distanceNew = [self getDistance:p1 toPoint:p2];
float dx = (distanceNew - initialDistance)/unitDistance;
double scale = fabs(mCurrentImageView.transform.a + dx);
[self animateFirstTouch:scale];
initialDistance = distanceNew;
}
else if([touches count]==1)
{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
CGFloat deltaX = gestureStartPoint.x - currentPosition.x;
CGFloat deltaY = gestureStartPoint.y - currentPosition.y;
mCurrentImageView.center = CGPointMake(wmsImageViewCenter.x - deltaX, wmsImageViewCenter.y - deltaY);
mCurrentImageView.opaque = YES;
mCurrentImageView.contentMode = UIViewContentModeScaleAspectFit;
mCurrentImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
}
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CGRect frameRect = [mCurrentImageView frame];
if(frameRect.size.height>Height)
{
contentSize.width=frameRect.size.width;
contentSize.height= frameRect.size.height;
mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
}
else
{
if (! mSwiping || [touches count] != 1)
return;
CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
mLeftImageView.frame = CGRectMake(swipeDistance - contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(swipeDistance,0.0f, contentSize.width, contentSize.height);
mRightImageView.frame = CGRectMake(swipeDistance + contentSize.width, 0.0f, contentSize.width, contentSize.height);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (! mSwiping)
return;
CGSize contentSize = self.frame.size;
NSUInteger count = [mImages count];
CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
CGRect frameRect = [mCurrentImageView frame];
if(frameRect.size.height>Height)
{
if (mCurrentImage > 0 && swipeDistance > 250.0f)
{
[mRightImageView removeFromSuperview];
mRightImageView.image=nil;
mRightImageView=nil;
result.image=nil;
result=nil;
mRightImageView = mCurrentImageView;
mCurrentImageView = mLeftImageView;
mCurrentImage--;
if (mCurrentImage > 0)
{
mLeftImageView = [self createImageView:mCurrentImage - 1];
mLeftImageView.hidden = YES;
[self addSubview:mLeftImageView];
result=nil;
}
else
{
mLeftImageView = nil;
result=nil;
}
[UIView beginAnimations:#"swipe" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3f];
CGSize contentSize = self.frame.size;
mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
[UIView commitAnimations];
mSwiping = NO;
}
//////////////////////////////////////////////////////////right Swaping ///////////////////////////////////////////////////////////////////////
else if (mCurrentImage < count - 1 && swipeDistance < -250.0f)
{
[mLeftImageView removeFromSuperview];
mLeftImageView.image=nil;
mLeftImageView=nil;
result.image=nil;
result=nil;
mLeftImageView = mCurrentImageView;
mCurrentImageView = mRightImageView;
mCurrentImage++;
if (mCurrentImage < count - 1)
{
mRightImageView = [self createImageView:mCurrentImage + 1];
mRightImageView.hidden = YES;
[self addSubview:mRightImageView];
result=nil;
}
else
{
mRightImageView=nil;
result=nil;
}
[UIView beginAnimations:#"swipe" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3f];
CGSize contentSize = self.frame.size;
mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
[UIView commitAnimations];
mSwiping = NO;
}
}
else
{
///////////////////////////////////////////////////// Left swaping////////////////////////////////////////////////////////////////////////////
if (mCurrentImage > 0 && swipeDistance > 50.0f)
{
[mRightImageView removeFromSuperview];
mRightImageView.image=nil;
mRightImageView=nil;
result.image=nil;
result=nil;
mRightImageView = mCurrentImageView;
mCurrentImageView = mLeftImageView;
mCurrentImage--;
if (mCurrentImage > 0)
{
mLeftImageView = [self createImageView:mCurrentImage - 1];
mLeftImageView.hidden = YES;
[self addSubview:mLeftImageView];
result=nil;
}
else
{
mLeftImageView = nil;
result=nil;
}
}
//////////////////////////////////////////////////////////right Swaping ///////////////////////////////////////////////////////////////////////
else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
{
[mLeftImageView removeFromSuperview];
mLeftImageView.image=nil;
mLeftImageView=nil;
result.image=nil;
result=nil;
mLeftImageView = mCurrentImageView;
mCurrentImageView = mRightImageView;
mCurrentImage++;
if (mCurrentImage < count - 1)
{
mRightImageView = [self createImageView:mCurrentImage + 1];
mRightImageView.hidden = YES;
[self addSubview:mRightImageView];
result=nil;
}
else
{
mRightImageView=nil;
result=nil;
}
}
[UIView beginAnimations:#"swipe" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.3f];
mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
mCurrentImageView.frame = CGRectMake(0.0f,0.0f, contentSize.width, contentSize.height);
mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);
[UIView commitAnimations];
mSwiping = NO;
}
}
///////////////////////////////////////////////////////////////////////////////////Tab Bar functionality /////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark === Animating subviews ===
#pragma mark
- (void)animateFirstTouch:(double)scale
{
if (mCurrentImageView != nil)
{
CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);
mCurrentImageView.transform = transform;
/*
CGSize contentSize = self.frame.size;
if(contentSize.width>1024)
{
//contentSize= CGSizeMake(1024, 1024);
mCurrentImageView.opaque = YES;
mCurrentImageView.contentMode = UIViewContentModeScaleAspectFit;
mCurrentImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
mCurrentImageView.clipsToBounds = YES;
//mCurrentImageView.frame = self.bounds;
[mCurrentImageView setNeedsLayout];
}
*/
}
}
- (double)getDistance:(CGPoint)fromPoint toPoint:(CGPoint)otherPoint
{
double deltaX = otherPoint.x - fromPoint.x;
double deltaY = otherPoint.y - fromPoint.y;
return sqrt(pow(deltaX, 2) + pow(deltaY, 2));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SlideShowView
#implementation SlideShowViewController
#synthesize Values,PreVal;
#synthesize ImageID,ImagePath,ImagePos,CatID,SlideID,CID;
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
int counter=0;
ImageID=[[NSMutableArray alloc]init];
ImagePath=[[NSMutableArray alloc]init];
ImagePos=[[NSMutableArray alloc] init];
CatID=[[NSMutableArray alloc] init];
SlideID=[[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:#"Category.sqlite3"];
sqlite3 *db;
if (sqlite3_open([writablePath UTF8String], &db) == SQLITE_OK)
{
sqlite3_stmt *statement = nil;
NSString *insertQuery=[NSString stringWithFormat:#"select *from Image where SlideID=%d and CatID=%d;",Values,CID];
const char *sql = [insertQuery cStringUsingEncoding:NSASCIIStringEncoding];
if(sqlite3_prepare_v2(db, sql, -1, &statement, NULL) != SQLITE_OK)
{
NSAssert1(0,#"error1",sqlite3_errmsg(db));
}
else
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
counter++;
[ImageID addObject:[NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,0)]];
[ImagePath addObject:[NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,1)]];
[ImagePos addObject:[NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,2)]];
[CatID addObject:[NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,3)]];
[SlideID addObject:[NSString stringWithFormat:#"%s",(char *)sqlite3_column_text(statement,4)]];
}
sqlite3_finalize(statement);
}
}
sqlite3_close(db);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Slide= [[[SlideShowView alloc] initWithImages:ImagePath] autorelease];
self.view=Slide;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
tabBar.alpha = 0.0;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{ AIX=510;
AIY=360;
Valtab=690;
ValScroll=730.0;
Height=1008;
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
AIX=510;
AIY=360;
Valtab=690;
ValScroll=730.0;
Height=750;
}
else if(interfaceOrientation == UIInterfaceOrientationPortrait)
{
AIX=360;
AIY=510;
Valtab=945;
ValScroll=530;
Height=1008;
}
else
{
AIX=360;
AIY=510;
Valtab=945;
ValScroll=530.0;
Height=750;
}
return YES; //(interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)dealloc {
[super dealloc];
[activityIndicator release];
}
#end
Try rearranging your init method a little:
- (id)initWithImages:(NSArray *)inImages
{
initialDistance = 1;
unitDistance = 1;
// remove these
// [self setMultipleTouchEnabled:YES];
// self.multipleTouchEnabled = YES;
if (self = [super initWithFrame:CGRectZero])
{
mImages = [inImages retain];
NSUInteger imageCount=[inImages count];
ImageVal=imageCount;
if (imageCount > 0)
{
mCurrentImageView = [self createImageView:0];
[self addSubview:mCurrentImageView];
result=nil;
if (imageCount > 1)
{
mRightImageView = [self createImageView:1];
[self addSubview:mRightImageView];
result=nil;
}
}
self.opaque = YES;
self.backgroundColor = [UIColor blackColor];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// move to here after the init on super is done
self.multipleTouchEnabled = YES;
}
return self;
}
Calling init on the super probably undoes your efforts to set it beforehand.

How do I display more than one page from a PDF?

Hello I want to make an application in which I have to display pdffile on iphone screen, which has a functionality of zooming. I have multiple pages of pdffile, but the problem is i can get display only one page.
Here is the code :
/*myView.m*/
#implementation MyView
- (void)configureTiledLayer {
if([global getfirsttime] == 0)
{
[global fetchpageCtr : 1];
[global fetchfirsttime:1];
}
zoom = 1.0f;
tiledLayer = [CATiledLayer layer];
TiledDelegate *delegate = [[TiledDelegate alloc] init];
tiledLayer.delegate = delegate;
// get tiledLayer size
CGRect pageRect = CGPDFPageGetBoxRect(delegate.map, kCGPDFCropBox);
int w = pageRect.size.width;
int h = pageRect.size.height;
NSLog(#"height==%d,weight=%d",h,w);
// get level count
int levels = 1;
while (w > 1 && h > 1) {
levels++;
w = w >> 1;
h = h >> 1;
}
NSLog(#"Layer create");
// set the levels of detail
tiledLayer.levelsOfDetail = levels;
// set the bias for how many 'zoom in' levels there are
tiledLayer.levelsOfDetailBias = 5;
// setup the size and position of the tiled layer
CGFloat width = CGRectGetWidth(pageRect);
CGFloat height = CGRectGetHeight(pageRect);
tiledLayer.bounds = CGRectMake(0.0f, 0.0f, width, height);
CGFloat x = width * tiledLayer.anchorPoint.x;
CGFloat y = -height * tiledLayer.anchorPoint.y;
tiledLayer.position = CGPointMake(x * zoom, y * zoom);
tiledLayer.transform = CATransform3DMakeScale(zoom, zoom, 1.0f);
// transform the super layer so things draw 'right side up'
CATransform3D superTransform = CATransform3DMakeTranslation(0.0f, self.bounds.size.height, 0.0f);
self.layer.transform = CATransform3DScale(superTransform, 1.0, -1.0f, 1.0f);
[self.layer addSublayer:tiledLayer];
[tiledLayer setNeedsDisplay];
moving = NO;
NSLog(#"in layer");
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor blueColor];
[self configureTiledLayer];
}
return self;
}
- (id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
[self configureTiledLayer];
}
return self;
}
- (void)setZoom:(CGFloat)newZoom {
zoom = newZoom;
tiledLayer.transform = CATransform3DMakeScale(zoom, zoom, 1.0f);
}
- (void)zoomIn {
[self setZoom:zoom * 2.0f];
}
- (void)zoomOut {
[self setZoom:zoom * 0.5f];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(touches.count == 1) {
previousPoint = [[touches anyObject] locationInView:self];
} else if(touches.count == 2) {
// pinch zoom
pinchZoom = YES;
NSArray *touches = [event.allTouches allObjects];
CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
previousDistance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(touches.count == 1) {
CGPoint currentPoint = [[touches anyObject] locationInView:self];
CGPoint delta = CGPointMake(currentPoint.x - previousPoint.x, currentPoint.y - previousPoint.y);
tiledLayer.position = CGPointMake(tiledLayer.position.x + delta.x * zoom,
tiledLayer.position.y + delta.y * zoom);
previousPoint = currentPoint;
moving = YES;
} else if(touches.count == 2) {
// pinch zoom stuff
NSArray *touches = [event.allTouches allObjects];
CGPoint pointOne = [[touches objectAtIndex:0] locationInView:self];
CGPoint pointTwo = [[touches objectAtIndex:1] locationInView:self];
CGFloat distance = sqrt(pow(pointOne.x - pointTwo.x, 2.0f) +
pow(pointOne.y - pointTwo.y, 2.0f));
CGFloat newZoom = fabs(zoom + (distance - previousDistance) / previousDistance);
[self setZoom:newZoom];
previousDistance = distance;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!moving) {
if(touches.count == 1) {
// realy should recenter on a click but I'm being lazy
if([[touches anyObject] tapCount] == 2) {
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self zoomOut];
} else {
[self performSelector:#selector(zoomIn) withObject:nil afterDelay:0.25];
}
}
} else {
moving = NO;
}
}
- (void)dealloc {
[tiledLayer release];
[super dealloc];
}
/*TiledDelegate.m*/
#implementation TiledDelegate
- (CGPDFDocumentRef)sfMuni {
if(NULL == sfMuni) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Hunting-TrappingSynopsis_0910" ofType:#"pdf"];
NSURL *docURL = [NSURL fileURLWithPath:path];
sfMuni = CGPDFDocumentCreateWithURL((CFURLRef)docURL);
}
return sfMuni;
}
- (CGPDFPageRef)map {
int temppageno = [global getpageCtr];
NSLog(#"page ctr ==%d ",temppageno);
if(NULL == map) {
map = CGPDFDocumentGetPage(self.sfMuni, temppageno);
}
return map;
}
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
NSLog(#"\ndrawLayer:inContext:");
NSLog(#"ctm = %#", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
NSLog(#"box = %#\n", NSStringFromCGRect(CGContextGetClipBoundingBox(ctx)));
CGContextDrawPDFPage(ctx, self.map);
}
- (void)dealloc {
CGPDFPageRelease(map);
CGPDFDocumentRelease(sfMuni);
[super dealloc];
}
/*TiledLayerAppDelegate*/
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
/*TiledLayerViewController*/
- (void)viewDidLoad
{
l_pagectr = 2;
UIButton *btn1 = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn1.frame = CGRectMake(0,0,70,50);
[btn1 setBackgroundColor: [UIColor whiteColor]];
btn1.exclusiveTouch = YES;
[btn1 setTitle:#"Next" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(NextPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
-(IBAction)NextPressed : (id)sender
{
[global fetchpageCtr : l_pagectr++];
MyView *myview1=[[MyView alloc]init];
}
#end
Here when I pressed Next button it will have to display me the nest page but will display me the same page.I also get the page referance in "CGPDFPageRef" incremented but not displayed.
Plz help me for this.
Why don't you just use UIWebView? It renders PDF and offers zoom controls. No need to reinvent the wheel. Here is documentation for UIWebView.