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 ...
随机推荐
- rar 7z文件打包
把D:\file目录下的所有东西打包为file.rar放到D:\目录下, Rar.exe是放在c盘根目录下 >>C:\Rar.exe a -k -r -s -m1 D:\file.rar ...
- Admin添加字段
后台扩展用户信息,注意要到settings里面进行设定,有关联和继承两种方式 首先的关联表可以关联到user表但,主键在user表当中,所以没法直接在user表当中看到相关信息,要是通过继承扩展的话, ...
- django-admin 设计User外键,设计model
设置外键 class profile_user(AbstractBaseUser, PermissionsMixin): company = models.ForeignKey(Company, de ...
- PHP 实现Session入库/存入redis
对于大访问量的站点使用默认的Session 并不合适,我们可以将其存入数据库.或者使用Redis KEY-VALUE数据存储方案 首先新建一个session表 CREATE TABLE `sessio ...
- $Java正则表达式基础整理
(一)正则表达式及语法简介 String类使用正则表达式的几个方法: 正则表达式支持的合法字符: 特殊字符: 预定义字符: 方括号表达式: 圆括号表达式:用于将多个表达式组成一个子表达式,可以使用或运 ...
- 学习C#之旅 冒泡排序,选择排序,插入排序,希尔排序[资料收集]
关于冒泡排序,选择排序,插入排序,希尔排序[资料收集] 以下资料来源与网络 冒泡排序:从后到前(或者从前到后)相邻的两个两两进行比较,不满足要求就位置进行交换,一轮下来选择出一个最小(或最大)的放到 ...
- 进程控制块PCB结构体 task_struct 描述
进程控制块,英文名(Processing Control Block),简称 PCB . 进程控制块是系统为了管理进程设置的一个专门的数据结构,主要表示进程状态. 每一个进程都对应一个PCB来维护进程 ...
- C#多线程学习之Thread
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 机器学习性能指标之ROC和AUC理解与曲线绘制
一. ROC曲线 1.roc曲线:接收者操作特征(receiveroperating characteristic),roc曲线上每个点反映着对同一信号刺激的感受性. 横轴:负正类率(false po ...
- Pytorch的gather用法理解
先放一张表,可以看成是二维数组 行(列)索引 索引0 索引1 索引2 索引3 索引0 0 1 2 3 索引1 4 5 6 7 索引2 8 9 10 11 索引3 12 13 14 15 看一下下面例子 ...