add uiview as subview to MKmapview using MKMapPoint - iphone

Is it possible to add uiview in MKMapPoint,The code i have tried is given below but it's doesn't work. Any help is appreciated.
-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
{
CGPoint point = [recognizer locationInView:self.myMapView];
CLLocationCoordinate2D tapPoint = [self.myMapView convertPoint:point toCoordinateFromView:self.view];
MKMapPoint myMapPoint = MKMapPointForCoordinate(tapPoint);
UIView *mymapview=[[UIView alloc]initWithFrame:CGRectMake(myMapPoint.x, myMapPoint.y, 20, 20)];
mymapview.backgroundColor=[UIColor greenColor];
[self.view addSubview:mymapview];
}
Thanks.

[yourmapview addSubview: mymapview];
Add your mymapview on the map view like the above.

Related

Not Getting Proper location of CALayer iOS

I am working CALayer in iOS added a sublayers in myView and trying to get touch event through touches began but i am getting touch event not at the location where i have drawn the layer, plz help me in correcting it if smthing is wrong.
UIView *myView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
myView.backgroundColor = [UIColor whiteColor];
magicButton_ = [UIButton buttonWithType:UIButtonTypeRoundedRect];
magicButton_.frame = CGRectMake(10., 10., 300., 44.);
[magicButton_ setTitle:#"Invoke Magic!" forState:UIControlStateNormal];
[magicButton_ addTarget:self action:#selector(toggleMoney:) forControlEvents:UIControlEventTouchUpInside];
[myView addSubview:magicButton_];
simpleLayer_ = [[CALayer alloc]init];
simpleLayer_.bounds = CGRectMake(0., 0., 150., 20.);
simpleLayer_.position = CGPointMake((myView.center.x),(myView.center.y));
simpleLayer_.name = #"redlayer";
simpleLayer_.delegate = self;
simpleLayer_.backgroundColor = [[UIColor redColor]CGColor];
[myView.layer addSublayer:simpleLayer_];
self.view = myView;
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint location = [[touches anyObject] locationInView:self.view];
CALayer *hitLayer = [self.view.layer hitTest:[self.view convertPoint:location fromView:nil]];
NSLog(#"Location %f %f",location.x,location.y);
[self displayInfo:hitLayer.name];
}
-(void)displayInfo:(NSString *)nameOfLayer
{
NSLog(#"%#",nameOfLayer);
}
2013-10-14 13:13:37.374 MyLayerProj[1750:11303] (null)
2013-10-14 13:13:38.468 MyLayerProj[1750:11303] Location 87.000000 269.000000
2013-10-14 13:13:38.469 MyLayerProj[1750:11303] (null)
2013-10-14 13:13:39.239 MyLayerProj[1750:11303] Location 112.000000 278.000000
2013-10-14 13:13:39.240 MyLayerProj[1750:11303] redlayer
2013-10-14 13:13:40.479 MyLayerProj[1750:11303] Location 119.000000 278.000000
I have also included the logs which i am getting while running the code.
The [self.view convertPoint:location fromView:nil] in your call to -[CALayer hitTest] means "convert location from the co-ordinate space of the window to the co-ordinate space of self.view".
However, location is already in the co-ordinate space of the view, because you obtained it from passing self.view to -[UITouch locationInView:].

UIScrollView - Prevent UIImageView as subview of UIScrollView from being dragged off screen

I have a UIImageView element as a subview of my main UIScrollView element.
I want the Image to fill out the whole screen at all times, but I can drag the edges a bit too far, so that the yellow "background" becomes visible.
If I lift my finger, the Image "bounces" back and fills out the screen correctly.
I want to prevent this dragging of the image off screen.
I do however want the image to bounce back once I drag it out of the "safety area".
This is my ScrollView initialization:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:CGRectMake(0, 0, frame.size.height, frame.size.width)];
if (self) {
[self initImageValues];
self.showsVerticalScrollIndicator = NO;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = YES;
self.decelerationRate = UIScrollViewDecelerationRateFast;
self.delegate = self;
self.backgroundColor = [UIColor yellowColor];
self.minimumZoomScale = 1.0;
self.maximumZoomScale = 2.0;
[self setCanCancelContentTouches:YES];
self.clipsToBounds = YES;
// Load Image
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
[self addSubview:imageView];
[self setContentSize: CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
// Set bigger "bounce" zone (safety area)
self.contentInset=UIEdgeInsetsMake(-SAFETY_ZONE,-SAFETY_ZONE,-SAFETY_ZONE,-SAFETY_ZONE);
self.scrollEnabled = YES;
}
return self;}
Use these delegate methods:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
Then read the offset and return if it's out of the "safety area".
Could be that you need another delegate method from the UIScrollView though, but a workaround like this should fix your problem :)
try setting bounces on the scrollview:
self.bounces = NO;
TRY this:
**
self.scrollView.maximumZoomScale = 1.0;
self.scrollView.minimumZoomScale = 1.0;
**
Try setting its bounces property to NO.

How Can I Move An UIImageView Around In A UIScrollView?

In my application, I have a UIImageView inside a UIScrollView. I want to zoom and drag the UIImageView. I managed to do the zooming but I can't seem to find a way to drag the image inside the UIScrollView using touchesMoved. I have Googled around but it seems that the only method I have found was only possible in iOS 3.0+ but is now obsolete in iOS 4.0+. So how can I drag an image with my finger that is inside a UIScrollView?
You shouldn't need to do any touch detection yourself. UIScrollView can take care of everything.
Here's a example:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.delegate = self;
[self.view addSubview:scrollView];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.jpg"]];
scrollView.contentSize = imageView.bounds.size;
[scrollView addSubview:imageView];
float minScale = scrollView.frame.size.width / imageView.frame.size.width;
scrollView.minimumZoomScale = minScale;
scrollView.maximumZoomScale = 2.0;
scrollView.zoomScale = minScale;
}
Don't forget to add this delegate method to enable zooming:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}

find which view was clicked

I have a view controller where there are several uiview object. I need to know on which uiview user have tapped. how is this possible? any guidance will help a lot....
Thanks
Pankaj
Here is what you can do to get what you wanted ..... In this example i have created 7 views
UITapGestureRecognizer* gestureRecognizer;
UIView* myView;
for (int i = 0; i < 8; i++)
{
gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doSomthing:)];
gestureRecognizer.numberOfTapsRequired = 1;//or what ever you want
myView = [[UIView alloc] initWithFrame:CGRectMake(10, i*30, 30, 28)];
myView.backgroundColor = [UIColor redColor];
myView.tag = 100+i;
[self.view addSubview:myView];
[myView addGestureRecognizer:gestureRecognizer];
[myView release];
[gestureRecognizer release];
}
Now you need to implement the method like this
-(void)doSomthing:(id)sender
{
UIView* temp = [(UITapGestureRecognizer*)sender view];
// here you get the view you wanted
NSLog(#"view number :%d",temp.tag);
}
I think this should help you
Set a tag for every view to keep track of them.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// We only support single touches, so anyObject retrieves just that touch from touches
UITouch *touch = [touches anyObject];
NSLog(#"view %i", [touch view].tag);
}
you can add gestures to the uiview objects to find which object has been touched. see the documentation.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html
for specific code just comment.
U can probably add a custom button with a tag on top of each view. Then u can know which view is tapped based on the button tag.
pls take a look at this. It may help.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/13041-touch-event-subview.html

how do i change the size of UIImageView according to my animationImages as they are bigger than initial UIImage

This is my code, i'm trying to drag and drop a card which will animate on touchesBegan, but the animationImages are larger than the initial card image before touches begin.
How can i change the UIImageView to accomdate the animateImages sizes?
cardAnimationArray is an array with 19 images xxx1.png, xxx2.png... which have various sizes.
cardMovedImage is the last image in the series of 19 images.
Thank you in advance!!
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:self.superview];
self.center = pt;
self.animationImages = [NSArray arrayWithArray:cardAnimationArray];
[cardAnimationArray release];
[self setAnimationRepeatCount:1];
self.animationDuration= 1;
[self startAnimating];
self.image = [UIImage imageNamed:cardMovedImageName];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
CGPoint pt = [[touches anyObject] locationInView:self.superview];
CGRect frame = [self frame];
frame.origin.x += pt.x - self.center.x;
frame.origin.y += pt.y - self.center.y;
[self setFrame: frame];
}
self.clipsToBounds = NO; is the solution
Set the scale mode to "Center"
Haha, there is actually a simple solution;
just set self.clipsToBounds = NO;
where self represents the UIImageView