ios开发小技巧(转)
1.通过下面方式可以获取图片的像素颜色点:
- (void*)getImageData:(UIImage*)image
{
void* imageData;
if (imageData == NULL)
imageData = malloc(4 * image.size.width * image.size.height);
CGColorSpaceRef cref = CGColorSpaceCreateDeviceRGB();
CGContextRef gc = CGBitmapContextCreate(imageData,
image.size.width,image.size.height,
8,image.size.width*4,
cref,kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(cref);
UIGraphicsPushContext(gc);
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
UIGraphicsPopContext();
CGContextRelease(gc);
return imageData;
}
[self.view addSubview:LY_Label];
LY_Label.backgroundColor = [UIColor clearColor];
LY_Label.text = @"密 码";
LY_Label.font= [UIFont fontWithName:@"zapfino" size:(15.0f)]; //字体设置
[[LY_Label layer] setBorderWidth:2.0f]; //边框设置
UITextField *LY_Text = [[UITextField alloc] initWithFrame:CGRectMake(143, 180, 80, 30) ];
[self.view addSubview:LY_Text];
LY_Text.backgroundColor = [UIColor whiteColor];
[LY_Text setBorderStyle:UITextBorderStyleLine]; //边框设置
LY_Text.placeholder = @"password"; //默认显示的字
LY_Text.font = [UIFont fontWithName:@"helvetica" size:12]; //字体和大小设置
LY_Text.textColor = [UIColor redColor]; //设置字体的颜色
LY_Text.clearButtonMode = UITextFieldViewModeWhileEditing; //清空功能x
LY_Text.returnKeyType = UIReturnKeyDone; //键盘有done
LY_Text.secureTextEntry = YES; //密码输入时
[LY_Text becomeFirstResponder]; //自动弹出软键盘
LY_Text.delegate = self; //托管
UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320,44)];
bar.tintColor = [UIColor blackColor];
UINavigationItem *barItem = [[UINavigationItem alloc] initWithTitle:@""];
[bar setItems:[NSArray arrayWithObjects:barItem,nil]];
[baseView addSubview:bar];
4.AnnotationView的一些操作
-
(MKAnnotationView
*)mapView:(MKMapView
*)aMapView
viewForAnnotation:(id <MKAnnotation>)annotation
{ if(![annotation
isKindOfClass:[MyAnnotation class]])
//
Don't mess
user location return
nil;
MKAnnotationView *annotationView
=
[aMapView
dequeueReusableAnnotationViewWithIdentifier:@"spot"]; if(!annotationView) {
annotationView = [[MKAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"spot"];
annotationView.rightCalloutAccessoryView = [UIButton
buttonWithType:UIButtonTypeDetailDisclosure]; [(UIButton
*)annotationView.rightCalloutAccessoryView
addTarget:self action:@selector(openSpot:)
forControlEvents:UIControlEventTouchUpInside];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.centerOffset = CGPointMake(7,-15);
annotationView.calloutOffset = CGPointMake(-8,0);
} // Setup annotation
view annotationView.image
=
[UIImage
imageNamed:@"pinYellow.png"];
//
Or
whatever return
annotationView;
}
- (MKAnnotationView
*)mapView:(MKMapView *)mapView
viewForAnnotation:(id
)annotation {
if (annotation == mapView.userLocation) {
return nil;
}
MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.rightCalloutAccessoryView = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];
pinView.animatesDrop = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 25, 25);
[button setTitle:[NSStringstringWithFormat:@"%f,%f",self.touchCordinate.latitude,self.touchCordinate.longitude]forState:UIControlStateNormal];
pinView.rightCalloutAccessoryView = button;
return pinView;
}
#define MERCATOR_RADIUS 85445659.44705395
- (int)getZoomLevel:(MKMapView*)_mapView {
return 21-round(log2(_mapView.region.span.longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * _mapView.bounds.size.width)));
}
- (void)mapView:(MKMapView *)_mapView regionDidChangeAnimated:(BOOL)animated {
NSLog(@"zoom level %d", [self getZoomLevel:_mapView]);
}
UILongPressGestureRecognizer *lpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self
action:@selector(longPress:)];
lpress.minimumPressDuration = 0.3;//按0.5秒响应longPress方法
lpress.allowableMovement = 10.0;
//给MKMapView加上长按事件
[self._mapView addGestureRecognizer:lpress];//mapView是MKMapView的实例
[lpress release];
}
return self;
}
- (void)longPress:(UIGestureRecognizer*)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ //这个状态判断很重要
//坐标转换
CGPoint touchPoint = [gestureRecognizer locationInView:self._mapView];
CLLocationCoordinate2D touchMapCoordinate =
[self._mapView convertPoint:touchPoint toCoordinateFromView:self._mapView];
self.touchCoordinate = touchMapCoordinate;
NSLog(@"location:%f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);
if (nil != self.newAnnotation) {
[self._mapView removeAnnotation:self.newAnnotation];
}
self.newAnnotation = [Annotation annotationWithCoordinate:self.touchCoordinate];
self.newAnnotation.title = [NSStringstringWithFormat:@"%f,%f",self.touchCoordinate.latitude,self.touchCoordinate.longitude];
if (nil != self.newAnnotation) {
[self._mapView addAnnotation:self.newAnnotation];
[self locationcircle:self.touchCoordinate radius:[circleScaleText.text doubleValue]];
}
//这里的touchMapCoordinate.latitude和touchMapCoordinate.longitude就是需要的经纬度,
//NSString *url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?latlng=%f,%f&sensor=false®ion=sh&language=zh-CN", touchMapCoordinate.latitude, touchMapCoordinate.longitude];
//[NSThread detachNewThreadSelector:@selector(loadMapDetailByUrl:) toTarget:self withObject:url];
}
}
//在鼠标点击位置添加固定大小的圈
-(void)locationcircle:(CLLocationCoordinate2D)coordinate radius:(CLLocationDistance)circleradius {
if (nil != self.locCircle) {
[self._mapView removeOverlay:locCircle];
}
locCircle = [MKCircle circleWithCenterCoordinate:coordinate radius:circleradius];
[self._mapView addOverlay:locCircle];
}
//改变位置圈的大小
- (IBAction)changecircleScale{
[self dismissWithClickedButtonIndex:0 animated:YES];
[self locationcircle:self.touchCoordinate radius:[circleScaleText.text doubleValue]];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if (annotation == mapView.userLocation) {
return nil;
}
MKPinAnnotationView *pinView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Pin"] autorelease];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.rightCalloutAccessoryView = [UIButtonbuttonWithType:UIButtonTypeDetailDisclosure];
pinView.animatesDrop = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 25, 25);
[button setTitle:[NSStringstringWithFormat:@"%f,%f",self.touchCoordinate.latitude,self.touchCoordinate.longitude]forState:UIControlStateNormal];
pinView.rightCalloutAccessoryView = button;
return pinView;
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if ([overlay isKindOfClass:[MKCircle class]])
{
MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay]autorelease];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
circleView.lineWidth = 3.0;
return circleView;
}
return nil;
}
- (int)getZoomLevel:(MKMapView*)mapView {
return 21-round(log2(mapView.region.span.longitudeDelta * MERCATOR_RADIUS * M_PI / (180.0 * mapView.bounds.size.width)));
}
//一个MKMapView的委托方法控制zoomlevel
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
//NSLog(@"zoom level %d", [self getZoomLevel:_mapView]);
}
- (void)setFrame:(CGRect)rect {
//[super setFrame:CGRectMake((320-rect.size.width)/2, 130, rect.size.width, 150)];
//self.center = CGPointMake(320/2, 480/2);
[super setFrame:CGRectMake(0, 20, 320, 460)];
}
- (void)layoutSubviews {
CGFloat buttonTop;
for (UIView *view in self.subviews) {
if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) {
view.frame = CGRectMake(view.frame.origin.x,
self.bounds.size.height - view.frame.size.height - 15,
view.frame.size.width,
view.frame.size.height);
buttonTop = view.frame.origin.y+200;
}
}
buttonTop -= 40;
NSLog(@"buttontop:%f",buttonTop);
circleLb.frame = CGRectMake(12, 50, 80, 30);
circleScaleText.frame = CGRectMake(100, 52, 150, 25);
changeBtn.frame = CGRectMake(260, 50, 40, 30);
}
7.mapView 定位当前位置使用CLLocationManager
#import "UserLoactionTestViewController.h"
@implementation UserLoactionTestViewController
- (void)loadView {
[super loadView];
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 10, 320, 400)];
[mapView setDelegate:self];
[mapView setMapType:MKMapTypeStandard];
[self.view addSubview:mapView];
[mapView release];
mapView.showsUserLocation=YES;
CLLocationManager *locationManager = [[CLLocationManager alloc] init];//创建位置管理器
locationManager.delegate=self;//设置代理
locationManager.desiredAccuracy=kCLLocationAccuracyBest;//指定需要的精度级别
//locationManager.distanceFilter=1000.0f;//设置距离筛选器
[locationManager startUpdatingLocation];//启动位置管理器
MKCoordinateSpan theSpan;
//地图的范围 越小越精确
theSpan.latitudeDelta= 0.05f;
theSpan.longitudeDelta=0.05f;
MKCoordinateRegion theRegion;
CLLocationCoordinate2D cr = locationManager.location.coordinate;
theRegion.center = cr; //[[locationManager location] coordinate];
theRegion.span = theSpan;
[mapView setRegion:theRegion];
//NSLog(@"%f %f",mapView.userLocation.location.coordinate.latitude,
// mapView.userLocation.location.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//NSLog(@"dfdfdfd");
mapView.region = MKCoordinateRegionMake(newLocation.coordinate, MKCoordinateSpanMake(0.005f, 0.005f));
[manager stopUpdatingHeading];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
@end
8.手指控制放大缩小(转的 没自己测验过,以后有需求的时候可以参考下)
CGRect scrollFrame = CGRectMake(20,90,280,280);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
scrollView.delegate = self;
UIImage *bigImage = [UIImage imageNamed:@"appleLogo.jpg"];
largeImageView = [[UIImageView alloc] initWithImage:bigImage];
[scrollView addSubview:largeImageView];
scrollView.contentSize = largeImageView.frame.size; //important!
[self.view addSubview:scrollView];
[scrollView release];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
MapAnnotation *annotation = [[MapAnnotationalloc] initWithcoordiante:coordinate];
[map_view addAnnotations: annotation];
[annotation release];
NSData *dataToWrite = [s dataUsingEncoding: NSUTF8StringEncoding];
NSFileHandle* outputFile = [NSFileHandle fileHandleForWritingAtPath:Log_FilePath];
[outputFile seekToEndOfFile]; //可以在文件的末尾继续写下去
[outputFile writeData:dataToWrite];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = @"cube";
transition.subtype = kCATransitionFromRight;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
DemoViewController *demoViewController =
[[DemoViewController alloc]
initWithNibName:@"DemoViewController"
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController: demoViewController animated:YES];
[demoViewController release];
简单介绍:.type 设置了主要的页面切换显示形式 cube 等
.subtype 设置了页面的旋转 左右上下 :~)
ios开发小技巧(转)的更多相关文章
- iOS开发小技巧 - UILabel添加中划线
iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...
- iOS开发小技巧 - runtime适配字体
iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...
- iOS开发小技巧 -- tableView-section圆角边框解决方案
[iOS开发]tableView-section圆角边框解决方案 tableView圆角边框解决方案 iOS 7之前,图下圆角边框很容易设置 iOS 7之后,tableviewcell的风格不再是圆角 ...
- iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧
1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...
- ios开发小技巧之提示音播放与震动
在ios开发中,有时候我们需要频繁播放某种提示声音,比如微博刷新提示音.QQ消息提示音等,对于这些短小且需要频繁播放的音频,最好将其加入到系统声音(system sound)里. 注意: 需要播放的音 ...
- 【转】IOS开发小技巧
1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...
- iOS开发小技巧--字典和数组的中文输出
一.在解析json数据的时候,得到的集合对象或者数组对象在用%@打印的时候回出现类似乱码的情况.如图: 在iOS中打印字典或者数组对象,系统会默认调用字典对象和数组对象的descriptionWith ...
- iOS开发小技巧--相机相册的正确打开方式
iOS相机相册的正确打开方式- UIImagePickerController 通过指定sourceType来实现打开相册还是相机 UIImagePickerControllerSourceTypeP ...
- iOS开发小技巧--iOS键盘 inputView 和 inputAccessoryView
iOS键盘 inputView 和 inputAccessoryView 1.inputAccessoryView UITextFields和UITextViews有一个inputAccessoryV ...
随机推荐
- linux list.h 移植
Linux内核中List链表的实现,对于想进阶的程序员来说,无疑是一个很好的学习机会.内核实现了一个功能十分强大的链表,而且是开源的,用在其他需要的地方岂不是很省事. 一.看List实现前,先补充ty ...
- 剑指offer 面试21题
面试21题: 题目:调整数组的顺序使奇数位于偶数前面 题一:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 解题思路:使用两个指针 ...
- 异动K线2--600532做一个分析时再给大家一只个股和近日大盘的分析
http://bbs.tianya.cn/post-stocks-612892-3.shtml ————看了一页就感觉没什么太大的意义 选时重于选股 这是一条股市生存的基本法则 看看天涯真正的高手 现 ...
- 最小化CentOS6.7(64bit)---安装mysql5.5、jdk、tomcat
********mysql******** ------------------------------------------------------------------------------ ...
- C++友元概念
采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,成员函数一般定义为公有的,依此提供类与外界间的通信接口. 但是,有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类 ...
- iOS UIScrollView 滚动到当前展示的视图居中展示
需求展示: 测试效果1 first uiscrollView 宽度 为屏幕宽度 滚动步长 为 scroll 宽度的1/3 分析: 这个是最普通版 无法使每一次滚动的结果子视图居中展示, WA ...
- jni 编译错误error: unknown type name '__va_list'
platforms\android-9\arch-arm\usr\include\stdio.h:257:37: error: unknown type name '__va_list' 解 ...
- ResourceLoader笔记
Ant路径匹配 Ant路径通配符支持“?”.“*”.“**”,注意通配符匹配不包括目录分隔符“/”: “?”:匹配一个字符,如“config?.xml”将匹配“config1.xml”: “*”:匹配 ...
- Docker 跨主机网络
Docker提供两种原生的跨主机网络: Overlay 和 Macvlan libnetwork & CNM libnetwork 是 docker 容器网络库,最核心的内容是其定义的 C ...
- MVC6 (ASP.NET5) 自定义TagHelper
1) 在 _ViewImports.cshtml 中引入TagHelper类所在的 Assembly . (注意不是namespace) : @addTagHelper "*, WebAp ...