让一个UIImageView响应点击事件
UIImageView *imgView =[[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 320 , 44 )];imgView. userInteractionEnabled = YES ;
UITapGestureRecognizer *singleTap =[[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector ( onClickImage )];
[imgView addGestureRecognizer :singleTap];
[singleTap release ];
-( void )onClickImage{
// here, do whatever you wantto do
NSLog ( @"imageview is clicked!" );
}
iphone调用系统电话、浏览器、地图、邮件等
openURL的使用方法:
view plaincopy toclipboardprint?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appString]];
其中系统的appString有:
view plaincopy toclipboardprint?
1.Map http://maps.google.com/maps?q=Shanghai
2.Email mailto://myname@google.com
3.Tel tel://10086
4.Msg sms://10086
openURL能帮助你运行Maps,SMS,Browser,Phone甚至其他的应用程序。这是Iphone开发中我经常需要用到的一段代码,它仅仅只有一行而已。
- (IBAction)openMaps {
//打开地图
NSString*addressText = @"beijing";
//@"1Infinite Loop, Cupertino, CA 95014";
addressText =[addressTextstringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString*urlText = [NSStringstringWithFormat:@"http://maps.google.com/maps?q=%@",addressText];
NSLog(@"urlText=============== %@", urlText);
[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:urlText]];
}
- (IBAction)openEmail {
//打开mail // Fire off an email to apple support
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
}
- (IBAction)openPhone {
//拨打电话
// CallGoogle 411
[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"tel://8004664411"]];
}
- (IBAction)openSms {
//打开短信
// Text toGoogle SMS
[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"sms://466453"]];
}
-(IBAction)openBrowser {
//打开浏览器
// Lanuch any iPhone developers fav site
[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"http://itunesconnect.apple.com"]];
}
iphone程序内调用谷歌地图
使用CLLocationManager类,MKMapView。并且实现<MKMapViewDelegate,CLLocationManagerDelegate>
//初始化CLLocationManager,CLLocationManager获得当前地理坐标
locmanager=[[CLLocationManager alloc]init];
[locmanager setDelegate:self];
//设置精确度
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanagerstartUpdatingLocation];
执行完以后,会自动调用代理方法:
在代理方法:
- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation{
//初始化矩形大小
CGRect rect=CGRectMake(0,0,320,460);
//设置地图大小为矩形大小
map=[[MKMapView alloc] initWithFrame:rect];
CLLocationCoordinate2Dloc=[newLocation coordinate];
lat=loc.latitude;
lon=loc.longitude;
//coordinate坐标
CLLocationCoordinate2DtheCoordinate;
CLLocationCoordinate2DtheCenter;
//theCoordinate.latitude=lat;
//theCoordinate.longitude=lon;
theCoordinate=loc;
[map setDelegate:self];
//设置地图显示的类型,有卫星地图,道路,等
[map setMapType:MKMapTypeStandard];
//[mapsetMapType:MKMapTypeSatellite];
//区域坐标Region(区域,地域)
MKCoordinateRegiontheRegin;
//theCenter.latitude=lat;
//theCenter.longitude=lon;
theCenter=loc;
theRegin.center=theCenter;
//坐标间距(span:间隔,间距)
MKCoordinateSpantheSpan;
theSpan.latitudeDelta=0.1;
theSpan.longitudeDelta=0.1;
//设置地图显示的区域,
theRegin.span=theSpan;
//[mapsetRegion:theRegin];
[map regionThatFits:theRegin];
map.showsUserLocation=YES;
[self.viewaddSubview:map];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapViewviewForAnnotation:(id<MKAnnotation>)annotation{
NSLog(@"-------viewForAnnotation-------");
//此类可以显示针一样的图标
MKPinAnnotationView*newAnnotation=[[MKPinAnnotationViewalloc] initWithAnnotation:annotationreuseIdentifier:@"annotation1"];
//newAnnotation.animatesDrop=YES;
//newAnnotation.animatesDrop=NO;
newAnnotation.pinColor=MKPinAnnotationColorPurple;
//显示标志提示
newAnnotation.canShowCallout=YES;
return newAnnotation;
}
本文介绍了如何在iOS应用程序中调用系统电话、浏览器、地图、邮件等功能,并提供了使用openURL方法的具体代码示例。同时,展示了如何在应用内调用谷歌地图API,通过CLLocationManager和MKMapView实现地图定位与显示。

被折叠的 条评论
为什么被折叠?



