Add callouts to custom infoMarkerWindow implementations for GMSMapview - uiview

I am using Google IOS SDK and have implemented
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker
to implement a custom view. When I show this view, it does not add a callout as Google map view normally does for its own markers. Can anyone please let me know how it would be possible to add this callout like pointer pointing towards the marker for this custom makerInfoWindow ?

Here is how to do it.
- (UIView *)mapView:(GMSMapView *)mapView markerInfoWindow:(GMSMarker *)marker{
int popupWidth = 300;
int contentWidth = 280;
int contentHeight = 140;
int contentPad = 10;
int popupHeight = 200;
int popupBottomPadding = 16;
int popupContentHeight = contentHeight - popupBottomPadding;
int buttonHeight = 30;
int anchorSize = 20;
CLLocationCoordinate2D anchor = marker.position;
CGPoint point = [mapView.projection pointForCoordinate:anchor];
UIView *outerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, popupHeight)];
float offSet = anchorSize * M_SQRT2;
CGAffineTransform rotateBy45Degrees = CGAffineTransformMakeRotation(M_PI_4); //rotate by 45 degrees
UIView *callOut = [[UIView alloc] initWithFrame:CGRectMake((popupWidth - offSet)/2.0, popupHeight - offSet, anchorSize, anchorSize)];
callOut.transform = rotateBy45Degrees;
callOut.backgroundColor = [UIColor blackColor];
[outerView addSubview:callOut];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, popupWidth, 190)];
[view setBackgroundColor:[UIColor whiteColor]];
view.layer.cornerRadius = 5;
view.layer.masksToBounds = YES;
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.borderWidth = 2.0f;
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, 80)];
[descriptionLabel setFont:[UIFont systemFontOfSize:12.0]];
descriptionLabel.numberOfLines = 5;
descriptionLabel.text = [marker snippet];
[view addSubview:titleLabel];
[view addSubview:descriptionLabel];
[outerView addSubview:view];
return outerView;
}

This method is a GMSMapViewDelegate method, did you set the delegate to the view controller?
mapView.delegate = self;

Related

UITapGestureRecognizer not working, Objective-C

I apologize for asking this question for the millionth time, I've read the other questions asking the same thing and I couldn't find the answer to my problem!
I made a UITapGestureRecognizer that is just not working, can someone take a look at my code and tell me what I'm doing wrong?
-(void) formatCellForY: (CGFloat) y{
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat cellHeight = 70;
self.cell = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"Cell"]];
[self.cell setFrame:CGRectMake(20, y, screen.size.width - 40, cellHeight)];
self.cell.backgroundColor = [UIColor grayColor];
self.cell.userInteractionEnabled = YES;
self.cell.layer.shadowColor = [UIColor blackColor].CGColor;
self.cell.layer.shadowOffset = CGSizeMake(0, 1);
self.cell.layer.shadowOpacity = 1.0;
self.cell.layer.shadowRadius = 1.0;
self.cell.layer.cornerRadius = 1.0;
self.cell.clipsToBounds = NO;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, self.cell.frame.size.width - 40, cellHeight*(.4))];
titleLabel.numberOfLines = 32;
titleLabel.text = self.program.programName;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.userInteractionEnabled = YES;
[self.cell addSubview:titleLabel];
UILabel *explanationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, titleLabel.frame.origin.y + titleLabel.frame.size.height, titleLabel.frame.size.width, cellHeight - (titleLabel.frame.origin.y+ titleLabel.frame.size.height))];
explanationLabel.text = self.program.programDescription;
explanationLabel.numberOfLines = 3;
explanationLabel.textColor = [UIColor whiteColor];
explanationLabel.font = [UIFont systemFontOfSize:10.0];
explanationLabel.userInteractionEnabled = YES;
[self.cell addSubview:explanationLabel];
self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(goToInfoPage:)];
self.tap.numberOfTapsRequired = 1;
self.tap.numberOfTouchesRequired = 1;
self.tap.enabled = YES;
[self.cell addGestureRecognizer:self.tap];
NSLog(#"%#", self.tap);
}
Here is the code I used to add the cell to the screen.
for (CKRecord *record in records) {
SBHProgram *program = [SBHProgram programForRecord:record];
SBHCell *cell = [SBHCell cellForProgram:program andY:90*i];
i++;
[scrollView addSubview:cell.cell];
}
You have missed adding self.cell to view. You are able to see labels with texts because you have added self.cell.clipsToBounds = NO;
All you have to do is add cell to view.
[self.view addSubview:self.cell]
PLEASE ADD
self.tap.delegate = self;

scrollView with paging enabled not working properly ? (Objective-C)

i want to create paging scrollView with three UIViews. And than wants to add imageView as a subView to these UIViews. i cant figure it out how to calculate the frame of imageViews.
When i run the code without imageViews it works perfectly fine.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];
NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"123.png"],[UIImage imageNamed:#"13.png"],[UIImage imageNamed:#"12.png"],nil];
for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
The frame for the image view needs to be relative to its superview (i.e. view) not the scroll view. To achieve the paging you desire change:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
to:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];

iOS FadeIn/FadeOut during paging

I have an UIScrollView with views inside that I can scroll horizontally. During the scroll I would like to add a fade out on the view which is going out of the screen and a fade in on the view which is going in the screen.
Here my code :
for (int i = 0; i <textEntries.count; i++)
{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i + 5;
frame.size.width = self.scrollView.frame.size.width - 10;
frame.size.height = self.scrollView.frame.size.height - 10;
self.scrollView.pagingEnabled = YES;
UIView *subview = [[UIView alloc] initWithFrame:frame];
if([[colorEntries objectAtIndex:i] isEqualToString:#"violet"]){
subview.backgroundColor = [UIColor colorWithRed:(147/255.0) green:(90/255.0) blue:(255/255.0) alpha:1];
} else {
subview.backgroundColor = [UIColor colorWithRed:(100/255.0) green:(166/255.0) blue:(255/255.0) alpha:1];
}
subview.layer.cornerRadius = 5;
[self.scrollView addSubview:subview];
UITextView *noteLabel = [[UITextView alloc] init];
noteLabel.frame = CGRectMake(0, 0, self.scrollView.frame.size.width - 20, self.scrollView.frame.size.height -20);
noteLabel.text = [textEntries objectAtIndex:i];
noteLabel.userInteractionEnabled = NO;
noteLabel.backgroundColor = [UIColor clearColor];
noteLabel.contentInset = UIEdgeInsetsMake(20,20,20,20);
noteLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:40];
[noteLabel setTextColor:[UIColor whiteColor]];
[subview addSubview:noteLabel];
[noteLabel addObserver:self forKeyPath:#"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
}
Could you help me to build this fadeIn/FadeOut effect ?
Thanks!

Not able to scroll images in iOS

I have create scrollview inside that i am displaying tiles using this i can swap images.if i take 9 images it displaying in scrollview but If i give setUserInteractionEnabled:NO means i can tiles i can swap but If i give setUserInteractionEnabled:YES then its scroll is working not tiles i can not swap image and not even touch images.Can any one tell me what is the mistake in this below code..
-(void)ViewDidLoad
{
thumbnailscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
thumbnailscrollview.delegate = self;
thumbnailscrollview.backgroundColor = [UIColor whiteColor];
[thumbnailscrollview setUserInteractionEnabled:YES];
[thumbnailscrollview setContentSize:CGSizeMake(320.0,1600.0)];
thumbnailscrollview.showsVerticalScrollIndicator = YES;
[self.view addSubview:thumbnailscrollview];
[self createTiles];
}
- (void)createTiles {
theviewlabel=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
// [self.view addSubview:thumbnailscrollview];
[thumbnailscrollview addSubview:theviewlabel];
tileindex=0;
UIColor *tileColors[] = {
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
[UIColor whiteColor],
};
int tileColorCount = sizeof(tileColors) / sizeof(tileColors[0]);
int rows=([_imageCollection1 count]/3)+1;
CGRect frame;
int count=0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < TILE_COLUMNS; col++) {
if ([_imageCollection1 count] <= count) {
return;
}
int index = (row * TILE_COLUMNS) + col;
frame = CGRectMake(TILE_MARGIN + col * (TILE_MARGIN + TILE_WIDTH),
TILE_MARGIN + row * (TILE_MARGIN + TILE_HEIGHT),
TILE_WIDTH, TILE_HEIGHT);
CGRect frame2 = CGRectMake(TILE_MARGIN + col * (TILE_MARGIN + TILE_WIDTH)-12,
TILE_MARGIN + row * (TILE_MARGIN + TILE_HEIGHT)-3,
12, 12);
tileFrame[index] = frame;
Tile *tile = [[Tile alloc] init];
tile.tileIndex = index;
tile.tag=[[_buttontagcollections1 objectAtIndex:index]intValue];
tile.mRearrangeView=[_imageCollection1 objectAtIndex:count];
count++;
tileForFrame[index] = tile;
tile.frame = frame;
tile.backgroundColor = tileColors[index % tileColorCount].CGColor;
//tile.cornerRadius = 8;
tile.delegate = self;
thelabel=[[UILabel alloc]initWithFrame:frame2];
thelabel.backgroundColor=[UIColor whiteColor];
thelabel.textColor=[UIColor redColor];
thelabel.layer.cornerRadius=7;
//button.titleLabel.textAlignment = NSTextAlignmentCenter;
thelabel.textAlignment=NSTextAlignmentCenter;
thelabel.text=[NSString stringWithFormat:#"%d",tileindex
];
thelabel.font=[UIFont systemFontOfSize:13];
//[self.view.layer addSublayer:tile];
[theviewlabel addSubview:thelabel];
homeView = [[UIView alloc]initWithFrame:CGRectMake(248, -480, 72, 480)];
homeView.backgroundColor = [UIColor clearColor];
[thumbnailscrollview addSubview:homeView];
[tile setNeedsDisplay];
tileindex++;
homeView = [[UIView alloc]initWithFrame:CGRectMake(248, -480, 72, 480)];
homeView.backgroundColor = [UIColor clearColor];
[thumbnailscrollview addSubview:homeView];
[thumbnailscrollview.layer addSublayer:tile];
}
}
}
enter code herewhy are you adjusting content size BEFORE adding subviews to scroll view.Always add content size after adding all subviews to scroll view.The content size of scroll view must be larger than its superview.
what you do :
in viewDIDLoad:
thumbnailscrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.viewframe.size.height)];
//[thumbnailscrollview setContentSize:CGSizeMake(320.0,1600.0)]; comment this line
and in createTile method, when you are all done adding your subview then set content size:
CGRect contentRect = CGRectZero;
for (UIView *view in self.thumbnailscrollview.subviews)
{
contentRect = CGRectUnion(contentRect, view.frame);
}
self.thumbnailscrollview.contentSize = contentRect.size;
it should work then.
thanks

How can i add multiple images to my scrollView in ios

i have scroll View with three slides,i want to add images to my scroll view.this is my code after this what do i need to add
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
If you want to add Images horizontally (say 3 images), than you have to add scroll view with the width of 3*yourImageView.frame.size.width and than you can add those Image on (x=0,y=0),
(x = yourImageView.frame.size.width, y=0), and (x = 2*yourImageView.frame.size.width, y=0)
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, yourImageView.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
imageView1.frame = CGrectMake(0,0,imageView1.frame.size.width,imageView1.frame.size.height);
imageView2.frame = CGrectMake(0,imageView1.frame.size.width,imageView2.frame.size.width,imageView2.frame.size.height)
imageView3.frame = CGrectMake(0,2*imageView1.frame.size.width,imageView3.frame.size.width,imageView3.frame.size.height)
[scroll addSubview:imageView1];
[scroll addSubview:imageView2];
[scroll addSubview:imageView3];
enable horizontal scrolling

Resources