I have ten local HTML files in my Resource folder and I want to performing paging effect like ScrollView and ImageView so please help me how I can do that. I have displayed HTML page in webView, but i don't have any more ideas. My code is (but it's for only One page):
arrHtmlPage=[[NSMutableArray alloc] initWithObjects:#"2",#"3",#"5",#"6",#"8",#"9",#"11",#"13",nil];
NSBundle *bundle = [NSBundle mainBundle];
NSString *htmlTemplateLink = [bundle pathForResource:#"2" ofType:#"htm"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlTemplateLink]];
wview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 768, 940)];
wview.scalesPageToFit = YES;
[wview loadRequest:request];
wview.delegate=self;
//wview.multipleTouchEnabled=YES;
wview.userInteractionEnabled=YES;
[self.view addSubview:wview];
const CGFloat kScrollObjHeight = 900;
const CGFloat kScrollObjWidth = 766;
const NSUInteger kNumImages = 11;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIWebView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
- (void)viewDidLoad {
[super viewDidLoad];
arrHtmlPage=[[NSMutableArray alloc] initWithObjects:#"",#"2",#"3",#"4",#"5",#"6",#"8",#"9",#"11",#"12",#"13",#"14",nil];
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
int i;
for (i = 0; i <[arrHtmlPage count]; i++)
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *htmlTemplateLink = [bundle pathForResource:[arrHtmlPage objectAtIndex:i] ofType:#"htm"];
NSLog(#"htmlTemplateLink==>%#",htmlTemplateLink);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlTemplateLink]];
wview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 766, 940) ];
wview.backgroundColor=[UIColor redColor];
wview.tag=i;
wview.scalesPageToFit = YES;
[wview loadRequest:request];
wview.userInteractionEnabled=YES;
[scrollView1 addSubview:wview];
}
[self layoutScrollImages];
}
-(IBAction)buttonPressed:(id)sender
{
int pageNo = [sender tag] ; // current page no
int tempNo = pageNo%10 + 1; // total count
CGPoint bottomOffset = CGPointMake((tempNo-1) * 768,0);
[scrollView1 setContentOffset:bottomOffset animated:YES];
}
In xcode 4.3.1 create a new project. File -> New project -> Page-Based application. Drag and drop a UIWebView onto the storyboard.
Related
I have a UIWebView that is always smaller than the device's screen. Whatever I tried, it just won't scale the website to fit its bounds.
Here is my code:
self.webView = [[UIWebView alloc] initWithFrame:
CGRectMake(.08 * width, .18 * height, .84 * width, .74 * height)];
self.webView.delegate = self;
self.webView.scrollView.delegate = self;
self.webView.scrollView.scrollEnabled = NO;
self.webView.scalesPageToFit = YES;
NSString *urlAddress = #"https://number26.de/app-tutorial/dist/new-feature-invite.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view.window addSubview:self.webView];
That is how the result looks like:
And this is how it SHOULD look like (Google Chrome / Dev mode):
okey, this is my code
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[UIWebView alloc] init];
self.webView.frame = CGRectMake(.08 * self.view.frame.size.width, .18 * self.view.frame.size.height, .84 * self.view.frame.size.width, .74 * self.view.frame.size.height);
self.webView.delegate = self;
self.webView.scrollView.delegate = self;
self.webView.scrollView.scrollEnabled = NO;
NSString *urlAddress = #"https://s.number26.de/app-tutorial/dist/new-feature-invite.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
[self.view addSubview:self.webView];
self.webView.scalesPageToFit = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self scaleToFit];
}
-(void)scaleToFit
{
if ([self.webView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[self.webView scrollView];
float zoom=self.webView .bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:YES];
}
}
and see full website in webView. ]
add this code
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[self scaleToFit];
}
-(void)scaleToFit
{
if ([self.webView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[self.webView scrollView];
float zoom=self.webView .bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:YES];
}
}
My main goal here is to achieve an effect like Readability or Safari's Reader service where the main content of the webpage is converted to text. I don't actually want to display any images, just get all the webpage's important text. I am currently using some pretty long self-built code to parse the webpage for the s to find out what the heading may look like, and I am also parsing the s that I hope contain the majority of the page's content.
-(void)interpretAndDisplay {
NSURL *URL = [NSURL URLWithString:self.url];
NSData *data = [NSData dataWithContentsOfURL:URL];
NSString *html = [NSString stringWithUTF8String:[data bytes]];
//Getting the H1s
NSMutableArray *h1Full = [[NSMutableArray alloc] init];
h1Full = [self stringsBetweenString:#"<h1" andString:#">" andText:html];
if ([h1Full count] > 0) {
NSMutableArray *h1Content = [[NSMutableArray alloc] init];
h1Content = [self stringsBetweenString:[NSString stringWithFormat:#"<h1%#>",[h1Full firstObject]] andString:#"</h1>" andText:html];
NSMutableArray *h1Sanitize = [[NSMutableArray alloc] init];
h1Sanitize = [self stringsBetweenString:#"<" andString:#">" andText:html];
if ([h1Content count] > 0) {
NSString *finalTitle = [h1Content firstObject];
for (int i = 0; i < [h1Sanitize count]; i++) {
NSString *toRemove = [NSString stringWithFormat:#"<%#>",[h1Sanitize objectAtIndex:i]];
finalTitle = [finalTitle stringByReplacingOccurrencesOfString:toRemove withString:#""];
finalTitle = [finalTitle stringByReplacingOccurrencesOfString:#"\n" withString:#""];
}
finalTitle = [self sanitizeString:finalTitle];
[self.titleLabel setText:finalTitle];
}
}
//Now for the body!
NSMutableArray *pTag = [[NSMutableArray alloc] init];
pTag = [self stringsBetweenString:#"<p" andString:#">" andText:html];
if ([pTag count] > 0) {
NSMutableArray *pContent = [[NSMutableArray alloc] init];
pContent = [self stringsBetweenString:[NSString stringWithFormat:#"<p%#>",[pTag firstObject]] andString:#"</p>" andText:html];
NSMutableArray *pSanitize = [[NSMutableArray alloc] init];
pSanitize = [self stringsBetweenString:#"<" andString:#">" andText:html];
if ([pContent count] > 0) {
for (int i = 0; i < [pContent count]; i++) {
NSString *pToEdit = [pContent objectAtIndex:i];
for (int i = 0; i < [pSanitize count]; i++) {
NSString *toRemove = [NSString stringWithFormat:#"<%#>",[pSanitize objectAtIndex:i]];
pToEdit = [pToEdit stringByReplacingOccurrencesOfString:toRemove withString:#""];
}
[pContent replaceObjectAtIndex:i withObject:pToEdit];
}
for (int i = 0; i < [pContent count]; i++) {
NSString *pToEdit = [pContent objectAtIndex:i];
pToEdit = [pToEdit stringByReplacingOccurrencesOfString:#"\n" withString:#""];
[pContent replaceObjectAtIndex:i withObject:pToEdit];
}
NSString *finalBody = #"";
for (int i = 0; i < [pContent count]; i++) {
if ([finalBody isEqualToString:#""]) {
finalBody = [NSString stringWithFormat:#"%#",[pContent objectAtIndex:i]];
}
else {
finalBody = [NSString stringWithFormat:#"%#\n\n%#",finalBody,[pContent objectAtIndex:i]];
}
}
finalBody = [self sanitizeString:finalBody];
[self.textLabel setText:finalBody];
}
}
}
The above code extracts all of the elements just fine and sanitizes them with a method that I created, but the problem is that just analyzing the P tags sometimes completely fails to simplify the content, and analyzing all possible content tags could mess with the content's order and layout.
Is there a better way or some framework that converts all the text into a nice string?
EDIT
Searching around, I found a Boilerpipe project that can extract text extremely easily (https://github.com/k-bx/boilerpipe/wiki/QuickStart). It looks as easy as this: String text= ArticleExtractor.INSTANCE.getText(url);
Can I do this on Objective C?
Edit 2
It appears that there is a boilerpipe API, but this has limited requests. I am mostly looking for a user-side solution.
Reggie is not the most tolerant approach in my opinion.
I'd try to find an existent open source (i.e. https://github.com/Kerrick/readability-js) and use WebKit to inject JS into a web page once loaded.
After that you can inject another JS, extracting the processed content (using the appropriate class from the source)
Then, using JavaScriptCore you can pass div's content to Objective-C (JS offers many ways of doing that)
I am centering a map around the user's location or alternatively around a default location with a zoom level of roughly 30 miles. The problem is I can zoom in but cannot zoom out.
Whenever I try to zoom out on my iPhone 5C, the map immediately zooms back in when I remove my finger pinch.
I tried adding the mapView methods regionDidChangeAnimated and didChangeDragState to make certain I was resetting the region and center of the view, but neither of those seem to changed the ability to zoom out.
Cannot figure out what I missing here. Thanks for your help!
//
// MapViewController.m
#import "MapViewController.h"
#import "MeetFetcher.h"
#import "MeetLocationAnnotation.h"
#import "MyAnnotations.h"
NSInteger const redPin = 0;
NSInteger const greenPin = 1;
NSInteger const purplePin = 2;
#interface MapViewController() <MKMapViewDelegate, CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#end
#implementation MapViewController
#synthesize mapView = _mapView;
#synthesize annotations = _annotations;
#synthesize delegate = _delegate;
#synthesize beginningDateForSearch = _beginningDateForSearch;
#synthesize levelOfCompetition = _levelOfCompetition;
#synthesize meets = _meets;
#synthesize myLocationMngr = _myLocationMngr;
#pragma mark - Synchronize Model and View
- (NSArray *)mapAnnotations
{
NSMutableArray *meetAnnotations = [NSMutableArray arrayWithCapacity:[self.meets count]];
NSInteger iCount = 0;
for (NSDictionary *meet in self.meets) {
NSLog(#"In [%# %#], iCount = %d and meet = %#", NSStringFromClass([self class]), NSStringFromSelector(_cmd), iCount, meet);
[meetAnnotations addObject:[MeetLocationAnnotation annotationForMeet:meet]];
iCount++;
NSLog(#"\n \n meetAnnotations = %#",meetAnnotations);
}
return meetAnnotations;
}
- (void)updateMapView
{
if (self.mapView.annotations) [self.mapView removeAnnotations:self.mapView.annotations];
if (self.annotations) [self.mapView addAnnotations:self.annotations];
}
- (void)setMapView:(MKMapView *)mapView
{
_mapView = mapView;
[self updateMapView];
}
- (void)setAnnotations:(NSArray *)annotations
{
_annotations = annotations;
[self updateMapView];
}
- (void)setMeets:(NSArray *)meets
{
if (_meets != meets) {
_meets = meets;
}
// Model changed, so update our View in map
NSLog(#"meets count in setMeets = %d", [meets count]);
NSArray* listOfAnnotations = [self mapAnnotations];
[self setAnnotations:listOfAnnotations];
[self updateMapView];
}
#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *aView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:#"MapVC"];
if (!aView) {
aView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"MapVC"];
aView.canShowCallout = YES;
aView.rightCalloutAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
aView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
aView.pinColor = myPinColor;
// could put a rightCalloutAccessoryView here
} else {
aView.annotation = annotation;
}
// [(UIImageView *)aView.rightCalloutAccessoryView setImage:nil]; // Could replace rightCallOutButton with image.
return aView;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)aView
{
UIImage *image = [self.delegate mapViewController:self imageForAnnotation:aView.annotation];
[(UIImageView *)aView.leftCalloutAccessoryView setImage:image];
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(#"callout accessory tapped for annotation %#", [view.annotation title]);
}
#pragma mark - View Controller Lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
self.myLocationMngr = [[CLLocationManager alloc] init];
self.myLocationMngr.delegate = self;
self.myLocationMngr.desiredAccuracy = 500; // 500 meters.
if( [CLLocationManager locationServicesEnabled] ) {
myPinColor = MKPinAnnotationColorGreen;
[self.myLocationMngr startUpdatingLocation];
} else {
CLLocationCoordinate2D defaultCoordinateWhenCLLocationDisabled;
defaultCoordinateWhenCLLocationDisabled.latitude = 45.194014;
defaultCoordinateWhenCLLocationDisabled.longitude = -117.862015;
MyAnnotations *annotation =
[[MyAnnotations alloc] initWithCoordinates:defaultCoordinateWhenCLLocationDisabled
title:DEFAULT_LOCATION_TITLE
subTitle:DEFAULT_LOCATION_SUBTITLE
selectedPinColor:redPin];
// [self.mapView addAnnotation:annotation];
[self mapView:self.mapView didAddAnnotationViews:(NSArray *)annotation];
}
//Set some paramater for the location object.
[self.myLocationMngr setDistanceFilter:kCLDistanceFilterNone];
[self.myLocationMngr setDesiredAccuracy:kCLLocationAccuracyBest];
//Set the first launch instance variable to allow the map to zoom on the user location when first launched.
firstLaunch=YES;
}
- (void)viewDidUnload
{
[super viewDidUnload];
[self setMapView:nil];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *lastLocation =[locations lastObject];
CLLocationAccuracy accuracy = [lastLocation horizontalAccuracy];
NSLog(#"Received location %# with accuracy %f", lastLocation, accuracy);
if(accuracy < 100.0)
{
MKCoordinateSpan span = MKCoordinateSpanMake(0.14, 0.14);
region = MKCoordinateRegionMake([lastLocation coordinate], span);
[_mapView setRegion:region animated:YES];
[manager stopUpdatingLocation];
}
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(MKAnnotationView *)annotation
{
//Zoom back to the user location after adding a new set of annotations.
//Get the center point of the visible map.
CLLocationCoordinate2D centre = [mv centerCoordinate];
NSLog(#"centerCoordinate.Latitude = %f and centerCoordinate.Longitude = %f",centre.latitude, centre.longitude);
// MKCoordinateRegion region;
//If this is the first launch of the app then set the center point of the map to the user's location.
if (firstLaunch) {
region = MKCoordinateRegionMakeWithDistance(_myLocationMngr.location.coordinate,50000,50000);
firstLaunch=NO;
}else {
//Set the center point to the visible region of the map and change the radius to match the search radius passed to the Google query string.
region = MKCoordinateRegionMakeWithDistance(centre,currentDist,currentDist);
}
//Set the visible region of the map.
[mv setRegion:region animated:YES];
}
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//Get the east and west points on the map so you can calculate the distance (zoom level) of the current map view.
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set your current distance instance variable.
currentDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
//Set your current center point on the map instance variable.
currentCentre = self.mapView.centerCoordinate;
region = MKCoordinateRegionMakeWithDistance(currentCentre,currentDist,currentDist);
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{
MKMapRect mRect = self.mapView.visibleMapRect;
MKMapPoint eastMapPoint = MKMapPointMake(MKMapRectGetMinX(mRect), MKMapRectGetMidY(mRect));
MKMapPoint westMapPoint = MKMapPointMake(MKMapRectGetMaxX(mRect), MKMapRectGetMidY(mRect));
//Set your current distance instance variable.
currentDist = MKMetersBetweenMapPoints(eastMapPoint, westMapPoint);
//Set your current center point on the map instance variable.
currentCentre = self.mapView.centerCoordinate;
region = MKCoordinateRegionMakeWithDistance(currentCentre,currentDist,currentDist);
}
- (IBAction)refresh:(id)sender
{
// might want to use introspection to be sure sender is UIBarButtonItem
// (if not, it can skip the spinner)
// that way this method can be a little more generic
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:spinner];
dispatch_queue_t downloadQueue = dispatch_queue_create("meet downloader", NULL);
dispatch_async(downloadQueue, ^{
NSArray *meetsWithCoordinates = [MeetFetcher selectedGeoreferencedMeets:_beginningDateForSearch andCompetitionLevel:_levelOfCompetition];
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationItem.leftBarButtonItem = sender;
NSLog(#"meetsWithCoordinates count = %d", [meetsWithCoordinates count]);
// NSLog(#"%#",meetsWithCoordinates);
myPinColor = MKPinAnnotationColorPurple;
self.meets = meetsWithCoordinates;
});
});
}
#end
You shouldn't call didAddAnnotationViews, you should add the annotation and let iOS call the delegate method if it does indeed add the annotations.
Every time annotationViews are added you're changing the region. I suspect you meant to do that every time new annotations are added so add the setRegion call in the same place you addAnnotations. The views might be being added on a different sequence than you expect, for example panning or zooming out might reveal more annotations and thus the views for those annotations are drawn.
So here is what I learned using Anna and Craig's suggestions. First off I was using the wrong method to determine whether app had permission to use location services. Then I put a switch in to determine whether the initial zoom level had already been set.
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
self.myLocationMngr = [[CLLocationManager alloc] init];
self.myLocationMngr.delegate = self;
self.myLocationMngr.desiredAccuracy = 500; // 500 meters.
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
myPinColor = MKPinAnnotationColorGreen;
[self.myLocationMngr startUpdatingLocation];
if (!haveSetZoomLevel) {
CLLocation *currentLocation = [self.myLocationMngr location];
CLLocationCoordinate2D currentCoordinates = [currentLocation coordinate];
region = MKCoordinateRegionMakeWithDistance(currentCoordinates,50000,50000);
[self.mapView setRegion:region animated:YES];
haveSetZoomLevel = YES;
}
}
//Set some paramater for the location object.
[self.myLocationMngr setDistanceFilter:kCLDistanceFilterNone];
[self.myLocationMngr setDesiredAccuracy:kCLLocationAccuracyBest];
}
Then I used
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
// zoom to user's location
} else {
// zoom to a default location
}
}
I'm trying to use the project MosaicUI in my project,
the problem I'm facing is how to move from loading local images from a json file, to images from my web server ?
Did anyone tried this before may be? Thanks.
You can use AFNetWorking to load images from Websites.
Change the method setModule in MosaicDataView.m like this
-(void)setModule:(MosaicData *)newModule
{
module = newModule;
__weak UIImageView *weakImage = imageView;
__weak UIView *weakView = self;
__weak MosaicData *weakModule = module;
UIImage *image = [imageView loadImage:module.tg_id ofType:#"png"];
if (image)
{
[imageView setImage:image];
}
else
{
[imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:module.l_cover_url]] placeholderImage:[UIImage imageNamed:#"test.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[weakImage setImage:image];
CGSize imgFinalSize = CGSizeZero;
[weakImage saveImage:image withFileName:weakModule.tg_id ofType:#"png"];
if (image.size.width < image.size.height)
{
imgFinalSize.width = weakView.bounds.size.width;
imgFinalSize.height = weakView.bounds.size.width * image.size.height / image.size.width;
// This is to avoid black bars on the bottom and top of the image
// Happens when images have its height lesser than its bounds
if (imgFinalSize.height < weakView.bounds.size.height)
{
imgFinalSize.width = weakView.bounds.size.height * weakView.bounds.size.width / imgFinalSize.height;
imgFinalSize.height = weakView.bounds.size.height;
}
}
else
{
imgFinalSize.height = weakView.bounds.size.height;
imgFinalSize.width = weakView.bounds.size.height * image.size.width / image.size.height;
// This is to avoid black bars on the left and right of the image
// Happens when images have its width lesser than its bounds
if (imgFinalSize.width < weakView.bounds.size.width)
{
imgFinalSize.height = weakView.bounds.size.height * weakView.bounds.size.width / imgFinalSize.height;
imgFinalSize.width = weakView.bounds.size.width;
}
}
weakImage.frame = CGRectMake(0, 0, imgFinalSize.width, imgFinalSize.height);
weakImage.center = CGPointMake(weakView.frame.size.width/2, weakView.frame.size.height/2);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
NSLog(#"%#",error);
}];
}
// Set new title
NSInteger marginLeft = self.frame.size.width / 20;
NSInteger marginBottom = self.frame.size.height / 20;
titleLabel.text = module.title;
titleLabel.font = [self fontWithModuleSize];
CGSize newSize = [module.title sizeWithFont:titleLabel.font constrainedToSize:titleLabel.frame.size];
CGRect newRect = CGRectMake(10+marginLeft,
self.frame.size.height - newSize.height - marginBottom,
newSize.width-30,
newSize.height);
titleLabel.frame = newRect;
}
I'm currently creating and returning a custom view with the google maps ios SDK by setting delegate to self and using the following code.
#pragma mark - GMSMapViewDelegate
-(UIView*)mapView:(GMSMapView *)mapView markerInfoWindow:(id<GMSMarker>)marker {
int popupWidth = 200;
int contentWidth = 180;
int contentPad = 10;
int popupHeight = 140;
int popupBottomPadding = 16;
int popupContentHeight = popupHeight - popupBottomPadding;
int buttonHeight = 30;
UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupHeight)];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupContentHeight)];
[view setBackgroundColor:[UIColor whiteColor]];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 0, contentWidth, 22)];
[titleLabel setFont:[UIFont systemFontOfSize:17.0]];
titleLabel.text = [marker title];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentPad, 24, contentWidth, 20)];
[descriptionLabel setFont:[UIFont systemFontOfSize:11.0]];
descriptionLabel.text = [marker snippet];
[view addSubview:titleLabel];
[view addSubview:descriptionLabel];
UIButton *directionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
directionButton.frame = CGRectMake(contentPad, 45, contentWidth, buttonHeight);
[directionButton setTitle:#"Directions" forState:UIControlStateNormal];
[directionButton addTarget:self action:#selector(directionsPressed) forControlEvents:UIControlEventTouchDown];
UIButton *viewLocationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewLocationButton addTarget:self action:#selector(viewLocationPressed) forControlEvents:UIControlEventTouchUpInside];
[viewLocationButton setTitle:#"View Location" forState:UIControlStateNormal];
viewLocationButton.frame = CGRectMake(contentPad, 80, contentWidth, buttonHeight);
// handle bottom dealio
UIImage *bottomImage = [UIImage imageNamed:#"map-pointer-bottom"];
UIImageView *bottomView = [[UIImageView alloc] initWithFrame:CGRectMake((popupWidth / 2) - (bottomImage.size.width / 2), (popupContentHeight), bottomImage.size.width, bottomImage.size.height)];
[bottomView setImage:bottomImage];
[outerView addSubview:view];
[outerView addSubview:bottomView];
[outerView addSubview:directionButton];
[outerView addSubview:viewLocationButton];
ListItem *li = (ListItem*)[marker userData];
self.currentItem = li;
NSLog(#"List Item %# - %#", li.type, li.typeid);
return outerView;
}
-(void)directionsPressed {
NSLog(#"Directions Pressed");
}
-(void)viewLocationPressed {
NSLog(#"Location View Pressed");
}
- (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(id<GMSMarker>)marker {
NSLog(#"Tap Captured");
}
The didTapWindowOfMarker is being fired when i tap the custom view, but neither of the target methods for the buttons are being fired.
Any ideas for why this might be?
Possibly, as mentioned officially in documentation of Google Maps Android API, the below restriction regarding infowindows applies to Google Maps iOS SDK also :
Info window is not a live View, rather the view is rendered as an image onto the map. As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view. You are advised not to place interactive components — such as buttons, checkboxes, or text inputs — within your custom info window.
So basically clicking on any part of the infowindow will trigger only "didTapWindowOfMarker"
Swift 3.0 Solution
//empty the default infowindow
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}
// reset custom infowindow whenever marker is tapped
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
customInfoView.removeFromSuperview()
// customInfoView.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
self.view.addSubview(customInfoView)
// Remember to return false
// so marker event is still handled by delegate
return false
}
// let the custom infowindow follows the camera
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
if (locationMarker != nil){
let location = locationMarker.position
customInfoView.center = mapView.projection.point(for: location)
}
}
// take care of the close event
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
customInfoView.removeFromSuperview()
}
and make outlet of this view(customInfoWindow) in same controller which has mapView.
I got the idea from this link thanks to this developer Custom and interactive googlemaps(IOS SDK) infowindow
I have a UView and I'm adding a delegate to the view so that a UIButton calls a selector. For the google map, I don't do anything when I call
- (void) mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker;
However, I set my infowindow (my custom view) delegate to self, and call all my actions when I press the button that's linked to the selector.
UPDATE:
Here is the code I use in order to detect tap on buttons added to my infoWindows. I create a custom infoWindow with 2 fake buttons (they could actually replaced by images because they won't trigger any action) and I add a completely transparent overlay with 2 real buttons over the infoWindow. These buttons will trigger the actions.
And I use a few delegate methods or KVC in order to move the overlay when the infoWindow itself is moved.
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker {
[self.actionOverlayCalloutView removeFromSuperview];
UIView *calloutView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight)];
float offset = anchorSize * M_SQRT2;
CGAffineTransform rotateBy45Degrees = CGAffineTransformMakeRotation(M_PI_4);
UIView *arrow = [[UIView alloc] initWithFrame:CGRectMake((infoWindowWidth - anchorSize)/2.0, infoWindowHeight - offset, anchorSize, anchorSize)];
arrow.transform = rotateBy45Degrees;
arrow.backgroundColor = [UIColor lightGrayColor];
[calloutView addSubview:arrow];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, infoWindowWidth, infoWindowHeight - offset/2)];
[contentView setBackgroundColor:[UIColor whiteColor]];
contentView.layer.cornerRadius = 5;
contentView.layer.masksToBounds = YES;
contentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
contentView.layer.borderWidth = 1.0f;
self.actionOverlayCalloutView =
[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:contentView]]; //hack to copy a view...
self.actionOverlayCalloutView.backgroundColor = [UIColor lightGrayColorWithAlpha:0.5];
self.actionOverlayCalloutView.layer.cornerRadius = 5;
NSMutableArray *falseButtons = [NSMutableArray array];
NSMutableArray *actionButtons = [NSMutableArray array];
PointMapItem *pointAnnotation = marker.userData;
if ([pointAnnotation canPerformSend]) {
UIButton *button = [[UIButton alloc] init];
[button setImage:[UIImage imageNamed:#"imageButton1.png"] forState:UIControlStateNormal];
[falseButtons addObject:button];
UIButton *activableButton = [[UIButton alloc] init];
[activableButton addTarget:self action:#selector(onButton1Clicked) forControlEvents:UIControlEventTouchUpInside];
[actionButtons addObject:activableButton];
}
if ([pointAnnotation canPerformShowDetails]) {
UIButton *button = [[UIButton alloc] init];
[button setImage:[UIImage imageNamed:#"imageButton1.png"] forState:UIControlStateNormal];
[falseButtons addObject:button];
UIButton *activableButton = [[UIButton alloc] init];
[activableButton addTarget:self action:#selector(onButton2Clicked) forControlEvents:UIControlEventTouchUpInside];
[actionButtons addObject:activableButton];
}
int buttonWidth = contentView.frame.size.width / [falseButtons count];
int currentOffset = 0;
for (int i=0; i<falseButtons.count; i++) {
UIButton *falseButton = [falseButtons objectAtIndex:i];
UIButton *activableButton = [actionButtons objectAtIndex:i];
[falseButton setFrame:CGRectMake(currentOffset, 0, buttonWidth, contentView.frame.size.height)];
currentOffset += buttonWidth;
activableButton.frame = falseButton.frame;
[activableButton setTitle:#"" forState:UIControlStateNormal];
[self.actionOverlayCalloutView addSubview:activableButton];
[contentView addSubview:falseButton];
}
[calloutView addSubview:contentView];
CLLocationCoordinate2D anchor = [self.mapView.selectedMarker position];
CGPoint point = [self.mapView.projection pointForCoordinate:anchor];
point.y -= self.mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2;
self.actionOverlayCalloutView.center = point;
[self.mapView addSubview:self.actionOverlayCalloutView];
return calloutView;
}
- (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {
if (pMapView.selectedMarker != nil && self.actionOverlayCalloutView.superview) {
CLLocationCoordinate2D anchor = [self.mapView.selectedMarker position];
CGPoint point = [self.mapView.projection pointForCoordinate:anchor];
float offset = anchorSize * M_SQRT2;
point.y -= self.mapView.selectedMarker.icon.size.height + offset/2 + (infoWindowHeight - offset/2)/2;
self.actionOverlayCalloutView.center = point;
} else {
[self.actionOverlayCalloutView removeFromSuperview];
}
}
- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
[self.actionOverlayCalloutView removeFromSuperview];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"mapView.selectedMarker"]) {
if (!self.mapView.selectedMarker) {
[self.actionOverlayCalloutView removeFromSuperview];
}
}
}
- (void)onButton2Clicked {
//your code
self.mapView.selectedMarker = nil;
}
- (void)onButton1Clicked {
// your code;
self.mapView.selectedMarker = nil;
}
FORMER POST:
Have a look at this thread and specifically at #9, should be helpful
https://code.google.com/p/gmaps-api-issues/issues/detail?id=4961
1)Create one subview which you want to show in infoWindow.
2)Set frame of subview equals to frame of infoWindow view.
[subView setFrame:infoview.frame];
subView = [[[NSBundle mainBundle] loadNibNamed:#"viewName" owner:self options:nil] objectAtIndex:0];
[self.mapview addSubview:subView];