首页 文章 精选 留言 我的

精选列表

搜索[ios],共10000篇文章
优秀的个人博客,低调大师

[IOS]自定义长触屏事件

写一个Demo来自定义一个长触屏事件,自定义长按手势。 实现步骤: 1.创建一个自定义手势类,命名为LongPressGestureRecognizer,在创建的时候继承UIGestureRecognizer LongPressGestuRecognizer.h: #import <UIKit/UIKit.h> @interface LongPressGestureRecognizer : UIGestureRecognizer @end LongPressGestuRecognizer.m: #import "LongPressGestureRecognizer.h" #import <UIKit/UIGestureRecognizerSubclass.h> #import <time.h> NSInteger timer1; NSInteger timer2; @implementation LongPressGestureRecognizer -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; NSDate *nowDate = [NSDate date]; NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"ss"]; timer1 = [[dateformatter stringFromDate:nowDate] integerValue]; [dateformatter release]; NSLog(@"%d",timer1); } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesEnded:touches withEvent:event]; NSDate *nowDate = [NSDate date]; NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"ss"]; [dateformatter release]; NSLog(@"%d",timer1); if ((timer2 -timer1) >= 2) { self.state = UIGestureRecognizerStateEnded; } } @end 2.修改主ViewController ViewController.h: #import <UIKit/UIKit.h> @interface DXWViewController : UIViewController<UIGestureRecognizerDelegate> @end ViewController.m: #import "DXWViewController.h" #import "LongPressGestureRecognizer.h" @interface DXWViewController () @end @implementation DXWViewController - (void)viewDidLoad { [super viewDidLoad]; LongPressGestureRecognizer * longPress = [[LongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPress:)]; [self.view addGestureRecognizer:longPress]; } -(void)LongPress:(LongPressGestureRecognizer *)my { NSLog(@"OK"); } @end 3.ViewController中的触屏事件touchesBegan和自定义手势中的touchesBegan区别: ViewController中的touchesBegan是针对整个View而言的,而自定义中的手势是要绑定到某个特定的view,只针对这个view才相应的手势事件 本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366409,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS 集合的深复制与浅复制

对象拷贝有两种方式:浅复制和深复制。顾名思义,浅复制,并不拷贝对象本身,仅仅是拷贝指向对象的指针;深复制是直接拷贝整个对象内存到另一块内存中。 一图以蔽之 再简单些说:浅复制就是指针拷贝;深复制就是内容拷贝。 ________________________________________ 集合的浅复制(shallow copy) 集合的浅复制有非常多种方法。当你进行浅复制时,会向原始的集合发送retain消息,引用计数加1,同时指针被拷贝到新的集合。 现在让我们看一些浅复制的例子: 1 2 3 4 5 6 1.NSArray*shallowCopyArray=[someArraycopyWithZone:nil]; 2. 3.NSSet*shallowCopySet=[NSSetmutableCopyWithZone:nil]; 4. 5.NSDictionary*shallowCopyDict=[[NSDictionaryalloc]initWithDictionary:someDictionarycopyItems:NO]; ________________________________________ 集合的深复制(deep copy) 集合的深复制有两种方法。可以用initWithArray:copyItems:将第二个参数设置为YES即可深复制,如 1 1.NSDictionaryshallowCopyDict=[[NSDictionaryalloc]initWithDictionary:someDictionarycopyItems:YES]; 如果你用这种方法深复制,集合里的每个对象都会收到copyWithZone:消息。如果集合里的对象遵循NSCopying协议,那么对象就会被深复制到新的集合。如果对象没有遵循NSCopying协议,而尝试用这种方法进行深复制,会在运行时出错。copyWithZone:这种拷贝方式只能够提供一层内存拷贝(one-level-deepcopy),而非真正的深复制。 第二个方法是将集合进行归档(archive),然后解档(unarchive),如: 1 1.NSArray*trueDeepCopyArray=[NSKeyedUnarchiverunarchiveObjectWithData:[NSKeyedArchiverarchivedDataWithRootObject:oldArray]]; ________________________________________ 集合的单层深复制(one-level-deep copy) 看到这里,有同学会问:如果在多层数组中,对第一层进行内容拷贝,其它层进行指针拷贝,这种情况是属于深复制,还是浅复制?对此,苹果官网文档有这样一句话描述 This kind of copy is only capable of producing a one-level-deep copy. If you only need a one-level-deep copy... If you need a true deep copy, such as when you have an array of arrays... 从文中可以看出,苹果认为这种复制不是真正的深复制,而是将其称为单层深复制(one-level-deepcopy)。因此,网上有人对浅复制、深复制、单层深复制做了概念区分。 浅复制(shallowcopy):在浅复制操作时,对于被复制对象的每一层都是指针复制。 深复制(one-level-deepcopy):在深复制操作时,对于被复制对象,至少有一层是深复制。 完全复制(real-deepcopy):在完全复制操作时,对于被复制对象的每一层都是对象复制。 当然,这些都是概念性的东西,没有必要纠结于此。只要知道进行拷贝操作时,被拷贝的是指针还是内容即可。 ________________________________________ 系统对象的copy与mutableCopy方法 不管是集合类对象,还是非集合类对象,接收到copy和mutableCopy消息时,都遵循以下准则: copy返回imutable对象;所以,如果对copy返回值使用mutable对象接口就会crash; mutableCopy返回mutable对象; 下面将针对非集合类对象和集合类对象的copy和mutableCopy方法进行具体的阐述 1、非集合类对象的copy与mutableCopy 系统非集合类对象指的是NSString,NSNumber...之类的对象。下面先看个非集合类immutable对象拷贝的例子 1 2 3 1.NSString*string=@ "origin" ; 2.NSString*stringCopy=[stringcopy]; 3.NSMutableString*stringMCopy=[stringmutableCopy]; 通过查看内存,可以看到stringCopy和string的地址是一样,进行了指针拷贝;而stringMCopy的地址和string不一样,进行了内容拷贝; 再看mutable对象拷贝例子 1 2 3 4 5 6 7 8 9 1.NSMutableString*string=[NSMutableStringstringWithString:@ "origin" ]; 2. //copy 3.NSString*stringCopy=[stringcopy]; 4.NSMutableString*mStringCopy=[stringcopy]; 5.NSMutableString*stringMCopy=[stringmutableCopy]; 6. //changevalue 7.[mStringCopyappendString:@ "mm" ]; //crash 8.[stringappendString:@ "origion!" ]; 9.[stringMCopyappendString:@ "!!" ]; 运行以上代码,会在第7行crash,原因就是copy返回的对象是immutable对象。注释第7行后再运行,查看内存,发现string、stringCopy、mStringCopy、stringMCopy四个对象的内存地址都不一样,说明此时都是做内容拷贝。 综上两个例子,我们可以得出结论: 在非集合类对象中:对immutable对象进行copy操作,是指针复制,mutableCopy操作时内容复制;对mutable对象进行copy和mutableCopy都是内容复制。用代码简单表示如下: [immutableObjectcopy]//浅复制 [immutableObjectmutableCopy]//深复制 [mutableObjectcopy]//深复制 [mutableObjectmutableCopy]//深复制 2、集合类对象的copy与mutableCopy 集合类对象是指NSArray、NSDictionary、NSSet...之类的对象。下面先看集合类immutable对象使用copy和mutableCopy的一个例子: 1 2 3 1.NSArray*array=@[@[@ "a" ,@ "b" ],@[@ "c" ,@ "d" ]; 2.NSArray*copyArray=[arraycopy]; 3.NSMutableArray*mCopyArray=[arraymutableCopy]; 查看内容,可以看到copyArray和array的地址是一样的,而mCopyArray和array的地址是不同的。说明copy操作进行了指针拷贝,mutableCopy进行了内容拷贝。但需要强调的是:此处的内容拷贝,仅仅是拷贝array这个对象,array集合内部的元素仍然是指针拷贝。这和上面的非集合immutable对象的拷贝还是挺相似的,那么mutable对象的拷贝会不会类似呢?我们继续往下,看mutable对象拷贝的例子: 1 2 3 1.NSMutableArray*array=[NSMutableArrayarrayWithObjects:[NSMutableStringstringWithString:@ "a" ],@ "b" ,@ "c" ,nil]; 2.NSArray*copyArray=[arraycopy]; 3.NSMutableArray*mCopyArray=[arraymutableCopy]; 查看内存,如我们所料,copyArray、mCopyArray和array的内存地址都不一样,说明copyArray、mCopyArray都对array进行了内容拷贝。同样,我们可以得出结论: 在集合类对象中,对immutable对象进行copy,是指针复制,mutableCopy是内容复制;对mutable对象进行copy和mutableCopy都是内容复制。但是:集合对象的内容复制仅限于对象本身,对象元素仍然是指针复制。用代码简单表示如下: [immutableObjectcopy]//浅复制 [immutableObjectmutableCopy]//单层深复制 [mutableObjectcopy]//单层深复制 [mutableObjectmutableCopy]//单层深复制 这个代码结论和非集合类的非常相似。 这时候,是不是有人要问了,如果要对集合对象复制元素怎么办?有这疑问的同学不妨回头看看集合的深复制。 好了,深复制与浅复制就讲到这里。 ________________________________________ 最后说个题外的东西,在搜集资料的过程中,发现一个有可能犯错的点 1 2 1.NSString*str=@ "string" ; 2.str=@ "newString" ; 上面这段代码,在执行第二行代码后,内存地址发生了变化。乍一看,有点意外。按照C语言的经验,初始化一个字符串之后,字符串的首地址就被确定下来,不管之后如何修改字符串内容,这个地址都不会改变。但此处第二行并不是对str指向的内存地址重新赋值,因为赋值操作符左边的str是一个指针,也就是说此处修改的是内存地址。 所以第二行应该这样理解:将@"newStirng"当做一个新的对象,将这段对象的内存地址赋值给str。 我有如下的两个方法查看内存地址 pstr会打印对象本身的内存地址和对象内容 1 2 1.(lldb)pstr 2.(NSString*)$0=0x000000010c913680@ "a" po&str则打印的是引用对象的指针所在的地址 1 2 1.(lldb)po&str 2.0x00007fff532fb6c0 ________________________________________ 本文转自 卓行天下 51CTO博客,原文链接:http://blog.51cto.com/9951038/1772515,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS关于数组的那些小菜菜

//删除数组 [areaArrremoveAllObjects]; [industryArrremoveAllObjects]; [natureArrremoveAllObjects]; 用NSMutableDictionary来保存“名片”记录。那么这一次,我们扩展,并使用NSMutableArray来保存“菜单”信息。 那么,我们将在AddressBook类中加入相关的属性。 AddressBook.h文件中加入: NSMutableArray *_bookArray; @property(nonatomic,copy) NSMutableArray *bookArray; 在AddressBook.m文件中加入: @synthesize bookArray=_bookArray;生成相应的get,set方法。 修改响应初始化的方法来完成初始化: - (id)initWithName:(NSString *)name{ self=[super init]; if (self) { _bookName=[[NSString alloc] initWithString:name]; _bookArray=[[NSMutableArray alloc] init]; _bookDictionary=[[NSMutableDictionary alloc] init]; } return self; } 下面,就开始响应的NSMutableArray类的操作。 加入元素到NSMutableArray中: - (void)addCardForArray:(AddressCard *)newCard{ [_bookArray addObject:newCard]; } 使用时: NSString*aName=[[NSStringalloc]initWithString:@"Tom"]; NSString*aEmail=[[NSStringalloc]initWithString:@"Tom@yahoo.cn"]; intaSalary=1000; NSString*bName=[[NSStringalloc]initWithString:@"LiLei"]; NSString*bEmail=[[NSStringalloc]initWithString:@"LiLei@yahoo.cn"]; intbSalary=1100; NSString*cName=[[NSStringalloc]initWithString:@"Lucy"]; NSString*cEmail=[[NSStringalloc]initWithString:@"Lucy@yahoo.cn"]; intcSalary=900; NSString*dName=[[NSStringalloc]initWithString:@"HanMeimei"]; NSString*dEmail=[[NSStringalloc]initWithString:@"HanMeimei@yahoo.cn"]; intdSalary=1300; AddressCard*card1=[[AddressCardalloc]init]; AddressCard*card2=[[AddressCardalloc]init]; AddressCard*card3=[[AddressCardalloc]init]; AddressCard*card4=[[AddressCardalloc]init]; [card1setName:aNameandEmail:aEmailandSalary:aSalary]; [card2setName:bNameandEmail:bEmailandSalary:bSalary]; [card3setName:cNameandEmail:cEmailandSalary:cSalary]; [card4setName:dNameandEmail:dEmailandSalary:dSalary]; AddressBook *myBook=[[AddressBook alloc] init]; [myBook initWithName:@"MyBook"]; [myBook addCardForArray:card1];//添加元素 [myBook addCardForArray:card2]; [myBook addCardForArray:card3]; [myBook addCardForArray:card4]; 遍历NSMutableArray中的元素: - (void)printBookForArray{ for (AddressCard *theCard in _bookArray) { [theCard print]; } } 使用时: [myBook printBookForArray];//输出元素 查询NSMutableArray中的元素: - (AddressCard *)findCardForArray:(NSString *)theName{ for (AddressCard *theCard in _bookArray) { if ([[theCard name] caseInsensitiveCompare:theName]==NSOrderedSame) { return theCard; } } return Nil; } 使用时: AddressCard *theCard= [myBook findCardForArray:@"Tom"];//查找 if (theCard!=Nil) { [theCard print]; }else{ NSLog(@"No have"); } 删除NSMutableArray中的元素(一): - (void)removeCardForArray:(AddressCard *)theCard{ [_bookArray removeObjectIdenticalTo:theCard]; } 使用时:先查找,再删除 AddressCard *theCard= [myBook findCardForArray:@"Tom"];//查找 if (theCard!=Nil) { [myBookremoveCardForArray:theCard];//删除元素 }else{ NSLog(@"No have"); } 用这个方法,删除的对象为同一对象。 什么时同一对象?方法备注中已经说明。 所以,用removeObjectIdenticalTo方法,无法删除那些,属性值都相同,但是占有不同空间的对象。 不信,可以亲自试一试! [myBook printBookForArray];//输出元素 AddressCard *removedCard=[[AddressCard alloc] init]; //申明一个新的Card对象,该对象与card1,具有相同的值,但是占有不同的内存 [removedCard setName:aName andEmail:aEmail andSalary:aSalary]; [myBook removeCardForArray:removedCard];//删除元素 [myBook printBookForArray];//输出元素 通过,输出可以说明,并没有删除啊! 那我们要问?我只需要属性值相等,就可以删除。怎么办? 那就开始下边的操作把! 删除NSMutableArray中的元素(二): - (void)removeCardByisEqualForArray:(AddressCard *)theCard{ [_bookArray removeObject:theCard]; } 方法中提到,要重载isEqual方法。 因为我们要进行比较的元素是AddressCard;所以,在AddressCard类中重载isEqual方法: - (BOOL)isEqual:(AddressCard *)theCard{ if ([_name isEqualToString:theCard.name]==YES && [_email isEqualToString:theCard.email]==YES) { return YES; }else{ return NO; } } 使用时: [myBook removeCardByisEqualForArray:removedCard];//删除元素 首先,调用AddressCard类中重载isEqual方法,比较元素是否相等;相等则会删除;否则不删除。 顺便提及一下: containsObject方法和indexOfObject方法都是通过isEqual方法的比较结果来进行查找的。这两个方法,就不详细说明了。 NSMutableArray中元素的排序: - (void) sortBySalarysForArray{ [_bookArray sortUsingSelector:@selector(compareBySalary:)]; } 在AddressCard类中,编写compareBySalary方法 - (NSComparisonResult) compareBySalary:(id)element{ if (salary>[element salary]) { return NSOrderedDescending; }else if (salary<[element salary]) { return NSOrderedAscending; }else{ return NSOrderedSame; } } 本文转自 卓行天下 51CTO博客,原文链接:http://blog.51cto.com/9951038/1754231,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS中造成dealloc不调用的原因

问题描述 最近在一个项目中用到了地图,发现在地图页面和上一个页面间反复切换回出现内存爆增的情况,就像吃了炫迈一样根本停不下来(直到app内存爆表,app闪退收场)。造成这一结果的根本原因是地图的mapView没有释放,导致每次打开地图界面的时候内存中都重新加载了一个地图mapView。于是在网上搜索了一番找到了解决办法,只需要在地图的ViewController中dealloc方法中释放掉mapView就行了。具体代码如下 以上给出的方法确实是对的,可以解决反复切换地图页面和地图上一级页面内存暴增造成的闪退问题。但是这里要说的不是这个问题,而是一个新的问题,我在dealloc中打了断点,但是dealloc根本就没有执行,所以mapView也就根本就没有释放,内存还是一样在暴增。为什么ViewController已经被pop了,而ViewController的dealloc方法却没有被调用?(按理说ViewController被pop的时候它的dealloc的方法应该被调用才对)。 解决办法 通过Google搜索终于在晚上找到了答案(大家就不要用百度,想要快速准确的找到自己想要的答案推荐大家用google)。造成ViewController不释放的原因可能有很多。遇到dealloc不调用的时候只需要检查您的ViewController中是否存在以下几个问题: ViewController中存在NSTimer 如果你的ViewController中有NSTimer,那么你就要注意了,因为当你调用 时,这个target:self就增加了ViewController的return count,如果你不将这个timer invalidate,将别想调用dealloc。 ViewController中有关的代理 一个比较隐秘的因素,你去找找与这个类有关的代理,有没有强引用属性?比如一个代理的delegate应该是 assign 的现在是retain,(╯‵□′)╯︵┻━┻,就是这个,它会影响你不让你调用dealloc,不信,就试试吧。(这个我还没有遇到过)。 ViewController中有Block 这个就是我我上面不进入dealloc的真正原因,Block体内使用实例变量也会造成循环引用,使得拥有这个实例的对象不能释放。 例如你这个类叫OneViewController,有个属性是NSString *name; 如果你在block体中使用了self.name,那样子的话这个类就没法释放。 要解决这个问题,MRC下只需 ARC下将block 换为weak 目前我所知道的就以上三种情况,如果有什么错误的地方或者还存在的一些情况,欢迎大家来补充。 ================= UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeHMGoodsAddressSelectView)]; [_backboard addGestureRecognizer:tap]; tap = nil;//置空,否则没法dealloc释放资源 本文转自ljianbing51CTO博客,原文链接:http://blog.51cto.com/ljianbing/1871801,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS 数组里面取字典的值

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 NSArray *arrData = @[@ "1" ,@ "2" ,@ "3" ,@ "4" ]; NSArray *arrKey = @[@ "A" ,@ "B" ,@ "C" ,@ "D" ]; NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjects:arrData forKeys:arrKey]; NSArray *arrData1 = @[@ "5" ,@ "6" ,@ "7" ,@ "8" ]; NSArray *arrKey1 = @[@ "E" ,@ "F" ,@ "G" ,@ "H" ]; NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithObjects:arrData1 forKeys:arrKey1]; NSMutableArray *arr = [NSMutableArray arrayWithObjects:dic,dic1, nil]; for ( int i=0; i<[arr count]; i++) { for (id f in [arr objectAtIndex:i]) { NSLog(@ "value = %@" ,[[arr objectAtIndex:i] valueForKey:f]); NSLog(@ "key = %@" ,f); } } NSLog(@ "arr = %@" ,[arr description]); 结果: 本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1214724,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS实现图片的缩放和居中显示

直接上代码 // //MoveScaleImageController.h //MoveScaleImage // //Created byon 12-4-24. //Copyright (c) 2012年__MyCompanyName__. All rights reserved. // #import <UIKit/UIKit.h> #import "MoveScaleImageView.h" @interface MoveScaleImageController: UIViewController<UIScrollViewDelegate>{ UIScrollView *myScrollView; UIImageView *myImageView; } @property(retain,nonatomic)UIScrollView *myScrollView; @property(retain,nonatomic)UIImageView *myImageView; @end // //MoveScaleImageController.m //MoveScaleImage // //Created byon 12-4-24. //Copyright (c) 2012年__MyCompanyName__. All rights reserved. // #import "MoveScaleImageController.h" @interface MoveScaleImageController() @end @implementation MoveScaleImageController @synthesize myScrollView; @synthesize myImageView; -(void)dealloc{ [myScrollView release]; [myImageView release]; [super dealloc]; } -(void)loadView{ [super loadView]; self.view.backgroundColor = [UIColor lightGrayColor]; //UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(110, 200, 100, 50)]; //[btn setFrame:CGRectMake(110, 200, 100, 40)]; [btn setBackgroundColor:[UIColor whiteColor]]; [btn setTitle:@"点击查看图片" forState:UIControlStateNormal]; [btn.titleLabel setFont:[UIFont systemFontOfSize:13]]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; [btn release]; //下面是我要剪切区域的覆盖层 //if(self.centerOverLayView==nil) //{ //UIView *centerView=[[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 210)]; //self.centerOverLayView=centerView; //[centerView release]; //} //self.centerOverLayView.backgroundColor=[UIColor clearColor]; //self.centerOverLayView.layer.borderColor=[UIColor orangeColor].CGColor; //self.centerOverLayView.layer.borderWidth=2.0; //[self.view addSubview:self.centerOverLayView]; } -(void)clickEvent:(id)sender{ NSLog(@"***********clickeventad"); myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; if(self.myScrollView==nil) { UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; self.myScrollView=scrollView; [scrollView release]; } self.myScrollView.backgroundColor=[UIColor blueColor]; self.myScrollView.delegate=self; self.myScrollView.multipleTouchEnabled=YES; self.myScrollView.minimumZoomScale=1.0; self.myScrollView.maximumZoomScale=10.0; [self.view addSubview:self.myScrollView]; UIImage *_image = [UIImage imageNamed:@"image.jpg"]; CGFloat imageView_X = (_image.size.width > self.view.frame.size.width) ? self.view.frame.size.width : _image.size.width; CGFloat imageView_Y; CGFloat origin; if(_image.size.width > self.view.frame.size.width){ origin = self.view.frame.size.width/_image.size.width; imageView_Y = _image.size.height*origin; } myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-imageView_X)/2, (self.view.frame.size.height-imageView_Y)/2, imageView_X, imageView_Y)]; if(self.myImageView==nil) { UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; self.myImageView=imageView; [imageView release]; } //[myImageView setImage:_image]; UIImage *originImage=[[UIImage alloc]initWithCGImage:_image.CGImage]; [myImageView setImage:originImage]; //[myImageView setFrame:CGRectMake(0, 0, _image.size.width, _image.size.height)]; [self.myScrollView addSubview:self.myImageView]; UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)]; [closeBtn setBackgroundColor:[UIColor redColor]]; [closeBtn setAlpha:0.5]; [closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:closeBtn]; [closeBtn release]; //UIView *backView = [[UIView alloc] initWithFrame:CGRectInset(self.view.frame, 5, 5)]; //backView.alpha = 0.5; //backView.backgroundColor = [UIColor blackColor]; ////[self.view addSubview:backView]; // //UIImage* image=[UIImage imageNamed:@"image.jpg"]; //MoveScaleImageView*fileContent = [[MoveScaleImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)]; //[fileContent setImage:image]; // ////[backView addSubview:fileContent]; //[self.view addSubview:fileContent]; // //[backView release]; //[fileContent release]; } -(void)closeEvent:(id)sender{ [self.myImageView setHidden:YES]; [self.myScrollView setHidden:YES]; } #pragma mark UIScrollView delegate methods //实现图片的缩放 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ NSLog(@"**************viewForZoomingInScrollView"); return self.myImageView; } //实现图片在缩放过程中居中 - (void)scrollViewDidZoom:(UIScrollView *)scrollView { CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0; CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0; self.myImageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end 本文转自 卓行天下 51CTO博客,原文链接:http://blog.51cto.com/9951038/1772523,如需转载请自行联系原作者

优秀的个人博客,低调大师

ios16--自定义控件1

k控制器: // // XMGViewController.h #import <UIKit/UIKit.h> @interface XMGViewController : UIViewController @end // // XMGViewController.m #import "XMGViewController.h" #import "XMGShop.h" #import "XMGShopView.h" @interface XMGViewController () // 购物车 @property (weak, nonatomic) IBOutlet UIView *shopCarView; // 添加按钮 @property (weak, nonatomic) IBOutlet UIButton *addButton; // 删除按钮 @property (weak, nonatomic) IBOutlet UIButton *removeButton; /** 数据数组 */ @property (nonatomic, strong) NSArray *dataArr; @end @implementation XMGViewController /** * 懒加载 */ - (NSArray *)dataArr{ if (_dataArr == nil) { // 加载数据 // 1.获取全路径 NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"shopData.plist" ofType:nil]; self.dataArr = [NSArray arrayWithContentsOfFile:dataPath]; // 字典转模型 // 创建临时数组 NSMutableArray *tempArray = [NSMutableArray array]; for (NSDictionary *dict in self.dataArr) { // 创建shop对象 XMGShop *shop = [XMGShop shopWithDict:dict]; // 把模型装入数组 [tempArray addObject:shop]; } self.dataArr = tempArray; } return _dataArr; } // 初始化数据 - (void)viewDidLoad { [super viewDidLoad]; } /** * 添加到购物车 * * @param button 按钮 */ - (IBAction)add:(UIButton *)button { /***********************1.定义一些常量*****************************/ // 1.总列数 NSInteger allCols = 3; // 2.商品的宽度 和 高度 CGFloat width = 80; CGFloat height = 100; // 3.求出水平间距 和 垂直间距 CGFloat hMargin = (self.shopCarView.frame.size.width - allCols * width) / (allCols -1); CGFloat vMargin = (self.shopCarView.frame.size.height - 2 * height) / 1; // 4. 设置索引 NSInteger index = self.shopCarView.subviews.count; // 5.求出x值 CGFloat x = (hMargin + width) * (index % allCols); CGFloat y = (vMargin + height) * (index / allCols); /***********************2.创建一个商品*****************************/ /* 原来的分开创建: // 1.创建商品的view UIView *shopView = [[UIView alloc] init]; // 2.设置frame shopView.frame = CGRectMake(x, y, width, height); // 3.设置背景颜色 shopView.backgroundColor = [UIColor greenColor]; // 4.添加到购物车 [self.shopCarView addSubview:shopView]; // 5.创建商品的UIImageView对象 UIImageView *iconView = [[UIImageView alloc] init]; iconView.frame = CGRectMake(0, 0, width, width); iconView.backgroundColor = [UIColor blueColor]; [shopView addSubview:iconView]; // 6.创建商品标题对象 UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.frame = CGRectMake(0, width, width, height - width); titleLabel.backgroundColor = [UIColor yellowColor]; titleLabel.textAlignment = NSTextAlignmentCenter; // 居中 [shopView addSubview:titleLabel]; */ XMGShopView *shopView = [[XMGShopView alloc] init]; shopView.frame = CGRectMake(x, y, width, height); [self.shopCarView addSubview:shopView]; /***********************3.设置数据*****************************/ // 设置数据 XMGShop *shop = self.dataArr[index]; shopView.iconView.image = [UIImage imageNamed:shop.icon]; shopView.titleLabel.text = shop.name; /***********************4.设置按钮的状态*****************************/ button.enabled = (index != 5); // 5.设置删除按钮的状态 self.removeButton.enabled = YES; } /** * 从购物车中删除 * * @param button 按钮 */ - (IBAction)remove:(UIButton *)button { // 1. 删除最后一个商品 UIView *lastShopView = [self.shopCarView.subviews lastObject]; [lastShopView removeFromSuperview]; // 3. 设置添加按钮的状态 self.addButton.enabled = YES; // 4. 设置删除按钮的状态 self.removeButton.enabled = (self.shopCarView.subviews.count != 0); } @end 自定义控件; // // XMGShopView.h #import <UIKit/UIKit.h> @interface XMGShopView : UIView /** 图片控件 */ @property (nonatomic, weak) UIImageView *iconView;// [self addSubview:iconView];已经有强指针引用了,这里用weak /** 标题控件 */ @property (nonatomic, weak) UILabel *titleLabel; @end // // XMGShopView.m #import "XMGShopView.h" @interface XMGShopView () @end @implementation XMGShopView /** * 初始化子控件(不要设置frame,获取的宽度高度是0,在layoutSubviews可以拿到) * */ - (instancetype)init{ if (self = [super init]) { /* // 0.获取当前控件的尺寸 CGFloat width = self.frame.size.width; CGFloat height = self.frame.size.height; // NSLog(@"init:%f----%f", width, height); */ // 1.创建商品的UIImageView对象 UIImageView *iconView = [[UIImageView alloc] init]; // iconView.frame = CGRectMake(0, 0, width, width); iconView.backgroundColor = [UIColor blueColor]; [self addSubview:iconView]; self.iconView = iconView; // 2.创建商品标题对象 UILabel *titleLabel = [[UILabel alloc] init]; // titleLabel.frame = CGRectMake(0, width, width, height - width); titleLabel.backgroundColor = [UIColor yellowColor]; titleLabel.textAlignment = NSTextAlignmentCenter; // 居中 [self addSubview:titleLabel];//强指针引用了 self.titleLabel = titleLabel;//titleLabel变成全局的,不然layoutSubviews()方法里面获取不到, } return self; } /** * 布局子控件(可以拿到frame) */ - (void)layoutSubviews{ // 0.一定要调用super,保留父类系统的布局。否则父类的布局就没了。 [super layoutSubviews]; // 1.获取当前控件的尺寸 CGFloat width = self.frame.size.width;//这个frame是从外面shopView.frame = CGRectMake(x, y, width, height);获取的 CGFloat height = self.frame.size.height; // 2.设置子控件的frame self.iconView.frame = CGRectMake(0, 0, width, width); self.titleLabel.frame = CGRectMake(0, width, width, height - width); } @end bean: // // XMGShop.h #import <Foundation/Foundation.h> @interface XMGShop : NSObject /** 图片的名称 */ @property (nonatomic, copy) NSString *icon; /** 商品的名称 */ @property (nonatomic, copy) NSString *name; // 提供构造方法 /* - (instancetype)initWithIcon: (NSString *)icon name: (NSString *)name; + (instancetype)shopWithIcon: (NSString *)icon name: (NSString *)name; */ - (instancetype)initWithDict:(NSDictionary *)dict; + (instancetype)shopWithDict:(NSDictionary *)dict; @end // // XMGShop.m #import "XMGShop.h" @implementation XMGShop /* - (instancetype)initWithIcon:(NSString *)icon name:(NSString *)name{ if (self = [super init]) { self.icon = icon; self.name = name; } return self; } + (instancetype)shopWithIcon:(NSString *)icon name:(NSString *)name{ return [[self alloc] initWithIcon:icon name:name]; } */ - (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { self.icon = dict[@"icon"]; self.name = dict[@"name"]; } return self; } + (instancetype)shopWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict]; } @end 本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7466723.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

IOS开发需要知道的知识-RunLoops

什么是Run Loops Run Loops是与线程想关联的基础部分。一个Run Loop就是事件处理循环,它是用来调度和协调接收到的事件处理。使用Run Loop的目的,就是使得线程有工作需要做时可以忙碌起来,而当没有事可做时,又可以使得线程睡眠。 Run Loop管理不都是自动的。我们必须手动设计线程代码,在合适的时候来启动Run Loop,并回应到来的事件。Cocoa和Core Foundation都提供了run loop对象来帮助我们配置和管理线程的run loop。我们的应用没有必要显式地创建这些对象;每个线程,包括应用程序的主线程,都有一个与之关联的run loop。只有子线程才需要显式地运行其run loop。App会将自动配置和来运行主线程的run loop的任务作为应用程序启动处理的一部分。 对于想要更深入地了解Run Loop Objects,阅读NSRunLoop Class Reference、CFRunLoop Reference 一个Run Loop的结构 Run Loop就像它的名字一样,它使得线程进入事件循环,能对到来的事件启动事件处理。你的代码中提供了流程控制说一句来实现run loop实实在在的循环部分,换句话说,你的代码提供了while或者for循环来驱动run loop。在你的循环中,你使用run loop对象在事件到达时,运行事件处理的代码并调起已安装的处理程序。Run Loop接收来自两种不同类型的源(sources)的事件: 输入源:异步传递事件,通常是来自不同的线程或不同的应用的消息。输入源异步传递事件到对应的处理程序和在线程关联的NSRunLoop对象调起runUntilDate:方法来退出事件处理。 Timer源:同步地传递事件,发生在每个定时器调用或周期性地调用。Timer源传递事件到他们的处理程序,但是不会调用run loop来退出处理。 这两种源在事件到达时都使用应用程序特定的处理程序来处理事件。 如下图所示,展示了run loop和不同的源的概述结构。 image 除了处理输入源之外,run loops还发出关于run loop行为的通知。我们可以注册成为run loop的观察者,就可以接收这些通知和使用它在线程上做一些额外处理。我们可以使用Core Foundation在对应的线程上注册成为run loop的观察者。 Run Loop Modes Run Loop模式是一个监视输入源和定时器的集合和注册成为run loop的观察者的集合。每次要运行run loop,都需要显示或隐式地指定某种运行的mode。只有与这种指定的mode关联的源才会被监视和允许传递他们的事件,同样地,只有与这种模式关联的观察者都会收到run loop行为变化的通知。与其它模式想关联的源,直到随后在合适的模式通过循环后,都会接收到新的事件(比如,将timer加入run loop default模式下,当滚动时,timer不会收到回调,直到停止滚动回到default模式下)。 在我们的代码中,我们通过名称来唯一标识mode。在Cocoa和Core Foundation中都定义了default模式和几个常用的模式,都是通过字符串名称来指定。我们也可以自定义模式,但是我们需要手动添加至少一个input source/timers/observers。 我们可以通过使用mode来过滤掉我们不希望接收到来自不想要的通过run loop的源。大部分情况下,我们都是使用系统定义的default模式。对于子线程,我们可以使用自定义模式在关键性操作时阻止低优先级的源传递事件。注意:Modes是通过事件源来区分,而不是事件类型来区分。比如说,我们不能使用mode来匹配只有mouse-down事件或者只有键盘事件。我们可以使用modes来监听不同系统的端口,临时挂起定时器,甚至改变正在被监视的sources和run loop观察者。 表格.png Input Sources 输入源异步传递事件到你的线程。事件的源由输入源的类型来决定,也就是两种源中的其中一种: Port-based:基于端口号的输入源监听应用程序的Mach端口。 Custom Input Sources:自定义输入源监听自定义的事件源。 系统通常实现了这两种输入源。唯一的不同点是它们是如何被发出信号的。port-based源是由内核(kernel)自动发出信号,而custom sources必须手动从其它线程发出信号。 当我们创建输入源时,可以指定mode。Modes会影响任何时刻被监视的输入源。大部分情况下,我们都让run loop在default mode下运行,但是也可以指定自定义的mode。如果一个输入源不是当前所监视的model,它所产生的任何事件都会被保留直接进入正常的mode。 Port-Based Sources Cocoa和Core Foundation提供了内建支持,可以使用与port相关的对象和函数来创建基于端口的输入源。举个例子,在Cocoa中永远不需要手动创建输入源。我们只需要简单地创建一个port对象和使用NSPort的方法。port对象为我们处理所需要的输入源的创建和配置。 在Core Foundation中,我们必须手动创建port和source。在这两种情况下,我们可以使用与port opaque type关联的函数(CFMessagePortRef, or CFSocketRef) 来创建合适的对象。 Custom Input Sources 在Core Foundation中,要创建自定义输入源,我们必须使用与CFRunLoopSourceRef关联的函数。我们配置自定义输入源可以使用几个回调函数。Core Foundation会在不同点回调这些函数来配置source,处理任何到达的事件和销毁已从run loop移除的source。 除了定义在事件到达时自定义源的行为之外,我们也必须定义事件传递机制。这部分源运行在单独的线程,负责提供输入源的数据,当数据准备好可以处理时,signaling(通知相关线程)这个消息。事件传递机制是我们自己来决定,但是不需要过于复杂。 Cocoa Perform Selector Source 除了基于端口的源之外,Cocoa还定义了自定义输入源允许我们在任意线程上执行selector。就像port-based源一样,执行selector请求会在目标线程上序列化,以减少在同一个线程中出现多个方法同步执行的问题。与port-based源不同的是,执行selector源在执行完毕后会自动将自己从run loop中移除。 当执行在其它线程执行selector时,目标线程必须要有运行的run loop。当我们创建线程时,这意味着直到启动了run loop都会显式地执行selector代码。 Run Loop每次经过一个循环,就会处理队列中所有的selector,而不仅仅是处理一个。 方法和说明。 Timer Sources Timer源在未来设定的时间会同步地传递事件到你的线程。Timers是线程通知自己去做一些事情的一种方式。比如说,搜索框可以使用定时器来初始化在一定时间就自动搜索,以便提供更多地联想词给用户。 尽管它发送基于时间的通知,但定时器并不是一种实时的机制。像输入源一样,定时器只有与run loop的mode一样才会发送通知。如果timer在run loop中并不是所被监视的mode,它不会触发定时器,直到run loop的mode与timer所支持的mode一样。 同样地,如果run loop正在处理中,timer已经fire了,这时候会被中断,直到下一次通过run loop才会调志处理程序。如果run loop已经不再运行了,则timer永远不会再fire。 我们可以配置timer只产生事件一次或者重复产生。重复的timer会自动根据调度的firing time自动调度,而不是真实的firing time。比如说,如果一个timer在特定的时间调度,然后每5秒重复一次。如果firing time被延迟导致缺少一或多次调用,那么timer在缺失的周期中只会调用一次。 Run Loop Observers 与sources在适当时机异步或同步发出事件不同,observers在run loop本身执行期间,会在特定的地方发出。你可能需要到run loop observers去准备线程处理特定的事件或者在进入睡眠之前。我们可以通过以下事件来关联run loop observers: 进入runlooprunloop将要处理timer runloop将要处理输入源 runloop将要进入睡眠 runloop被唤醒,但是还没有处理事件 退出runloop 我们可以通过Core Foundation来添加run loop observers。要创建run loop observer,可以通过CFRunLoopObserverRef来创建新的实例。这个类型会跟踪你所定义的回调函数和所感兴趣的活动。 与timers类型,run-loop observers可以使用一次或者重复多次。一次性的observer会在fire之后自动从run loop移除,而重复性的observer会继续持有。 The Run Loop Sequence Of Events 本小节讲的是RunLoop事件顺序。每次运行它,你的线程的run loop处理待处理的事件和给所有attached observers发出通知。处理的顺序如下: 1.通知observersrunloop已经进入2.通知observerstimers准备要fire3.通知observers有不是基于port-based的输入源即将要fire4.fire任何已经准备好的non-port-based输入源5.如果port-based输入源准备好且等待fire,则立即处理这个事件。然后进入步骤96.通知observers线程即将进入睡眠7.让线程进入睡眠,直到以下任何一种事件到达: *port-based输入源有事件到达 *timerfire *runloop超时 *runloop被显式唤醒8.通知observers线程被唤醒9.处理待处理的事件: *如果用户定义的timerfired了,处理timer事件并重新启动循环。进入步骤2 *如果输入源fired了,则传递事件 *如果runloop被显式唤醒,但是又未超时,则重启循环,进入步骤210.通知observersrunloop退出 由于observer对timer和输入源的通知会在事件真正发生之前被传递,这样就产生了间隙。如果这个间隙是很关键的,那么我们可以通过使用sleep和awake-from-sleep通知来帮助我们纠正这个时间间隔问题。 When Would You Use A Run Loop? 什么时候应该使用run loop呢? 只有当我们需要创建子线程的时候,才会需要到显示地运行run loop。应用程序的主线程的run loop是应用启动的基础任务,在启动时就会自动启动run loop。所以我们不需要手动启动主线程的run loop。 对于子线程,我们需要确定线程是否需要run loop,如果需要,则配置它并启动它。我们并不问题需要启动run loop的。比如说,如果我们开一个子线程去执行一些长时间的和预先决定的任务,我们可能不需要启动run loop。Run loop是用于那么需要在线程中有更多地交互的场景。比如说,我们会在下面的任何一种场景中需要开启run loop: 使用端口源或者自定义输入源与其它线程通信 在线程中使用定时器 使用Cocoa中的任何performSelector…方法 保持线程来执行周期性的任务 Using Run Loop Objects Run Loop对象给添加输入源、定时器和观察者到run loop提供了主接口。每个线程都有一个单独的run loop与之关联(对于子线程,若没有调用过任何获取run loop的方法是不会有run loop的,只有调用过,才会创建或者直接使用)。 在Cocoa中,通过NSRunLoop来创建实例,在low-level应用中,可以使用CFRunLoopRef类型,它是指针。 Getting A Run Loop Object 通过以下两种方式来获取run loop对象: 在Cocoa中,使用[NSRunLoop currentRunLoop]获取 使用CFRunLoopGetCurrent()函数获取 配置RunLoop 在子线程运行run loop之前,你必须至少添加一种输入源或者定时器。如果run loop没有任何的源需要监视,它就会立刻退出。 除了添加sources之外,你还可以添加观察者来检测runloop不同的执行状态。要添加观察者,可以使用CFRunLoopObserverRef指针类型和使用CFRunLoopAddObserver函数来添加到run loop中。我们只能通过Core Foundation来创建run loop观察者,即使是Cocoa应用。 下面这段代码展示主线程如何添加观察者到run loop以及如何创建run loop观察者: -(void)threadMain{//应用程序使用垃圾收集,因此不需要autorelease池。 NSRunLoop*myRunLoop=[NSRunLoopcurrentRunLoop];//创建一个运行循环观察者并将它附加到运行循环。 CFRunLoopObserverContextcontext={0,self,NULL,NULL,NULL};CFRunLoopObserverRefobserver=CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities,YES,0,&myRunLoopObserver,&context);if(observer){CFRunLoopRefcfLoop=[myRunLoopgetCFRunLoop];CFRunLoopAddObserver(cfLoop,observer,kCFRunLoopDefaultMode); }//创建和安排计时器。 [NSTimerscheduledTimerWithTimeInterval:0.1target:self selector:@selector(doFireTimer:)userInfo:nilrepeats:YES];NSIntegerloopCount=10;do{//运行运行循环10次让计时器。 [myRunLooprunUntilDate:[NSDatedateWithTimeIntervalSinceNow:1]]; loopCount--; }while(loopCount); } 在为长时间存在的线程配置run-loop时,最好添加至少一个输入源来接收事件。尽管我们可以只用timer源,但是一旦timer调用后,经常会被invalidate,这会导致run loop退出。 Starting the Run Loop 只有子线程才有可能需要启动run loop。Run loop必须至少有一种输入源或者timer源来监视。如果没有任何源,则run loop会退出。 下面的几种方式可以启动run loop: 无条件地:无条件进入run loop是最简单的方式,但也是最不希望这么做的,因为这样会导致run loop会进入永久地循环。可以添加、删除输入源和timer源,但是只能通过kill掉run loop才能停止。而且还不能使用自定义mode。 限时:与无条件运行run loop不同,最好是给run loop添加一个超时时间。 在特定的mode:除了添加超时时间,还可以指定mode。 下面是运行run loop的一段代码: -(void)skeletonThreadMain{//建立一个autorelease如果不使用垃圾收集池。 BOOLdone=NO;//添加你的来源或计时器运行循环,做其他任何设置。 do{//启动运行循环但每个源处理后返回。 SInt32result=CFRunLoopRunInMode(kCFRunLoopDefaultMode,10,YES); //如果源明确停止运行循环,或如果没有源或计时器,然后退出。if((result==kCFRunLoopRunStopped)||(result==kCFRunLoopRunFinished)) done=YES; //检查任何其他退出条件和设置完成所需的变量。}while(!done); //清理代码。一定要释放任何生成自动分配池。 } Exiting the Run Loop 有两种方法使run loop在处理事件之前,退出run loop: 给runloop设定超时时间 告诉runloop要stop 设定超时时间是比较推荐的。我们可以通过CFRunLoopStop函数来停止run loop。 Thread Safety and Run Loop Objects Core Foundation中的Run Loop API是线程安全的(以CF开头的API),而Cocoa中的NSRunLoop不是线程安全的。 Configuring Run Loop Sources 下面是展示如何配置不同类型的输入源。 Defining a Custom Input Source 创建自定义输入源涉及到以下部分: 想要处理的输入源的信息 让感兴趣的客户端知道如何联系输入源的调度程序 执行任何客户端发送的请求处理程序 使输入源失效的取消程序 Timer Source NSRunLoop*myRunLoop=[NSRunLoopcurrentRunLoop]; //创建和安排第一个定时器。NSDate*futureDate=[NSDatedateWithTimeIntervalSinceNow:1.0];NSTimer*myTimer=[[NSTimeralloc]initWithFireDate:futureDate interval:0.1 target:self selector:@selector(myDoFireTimer1:) userInfo:nil[myRunLoopaddTimer:myTimerforMode:NSDefaultRunLoopMode]; 或者使用Core Foundation: CFRunLoopRefrunLoop=CFRunLoopGetCurrent();CFRunLoopTimerContextcontext={0,NULL,NULL,NULL,NULL};CFRunLoopTimerReftimer=CFRunLoopTimerCreate(kCFAllocatorDefault,0.1,0.3,0,0, &myCFTimerCallback,&context);CFRunLoopAddTimer(runLoop,timer,kCFRunLoopCommonModes); 最后 本篇文章主要是官方文档的部分翻译版本,不过有很多无关的都省略了,而且有都转换成笔者的语言来表达出来,如果读不懂,最好还是去看官方文档吧。毕竟,英文与中文翻译不管怎么翻译都存在很大的问题。 疑问 官方文档中提到,每个线程都有一个run loop与之关联。但是实质上子线程在没有访问过run loop时,是不存在的。当访问时,若不存在则创建run loop并放到全局数组中。 本文转自 卓行天下 51CTO博客,原文链接:http://blog.51cto.com/9951038/1766775,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS上使用自定义ttf字体

项目中想使用第三方的字体,在stackoverflow上查询解决办法,也折腾一会,添加成功,示例如下: 1.将xx.ttf字体库加入工程里面 2.在工程的xx-Info.plist文件中新添加一行Fonts provided by application,加上字体库的名称 3.引用字体库的名称,设置字体:[UIFontfontWithName:@"fontname"size:24]; 1 2 3 4 5 UILabel*label=[[UILabelalloc]initWithFrame:CGRectMake(10,100,300,50)]; label.text=@ "这是一个TEST。123456" ; UIFont*font=[UIFontfontWithName:@ "文鼎CS中等線" size:24]; label.font=font; [self.viewaddSubview:label]; 如果不知道字体是名称,可以遍历字体进行查询: 1 2 3 4 5 6 7 8 9 for (NSString*fontfamilynamein[UIFontfamilyNames]) { NSLog(@ "family:'%@'" ,fontfamilyname); for (NSString*fontNamein[UIFontfontNamesForFamilyName:fontfamilyname]) { NSLog(@ "\tfont:'%@'" ,fontName); } NSLog(@ "-------------" ); } 示例Demo下载地址:http://down.51cto.com/3911492/up 参考http://stackoverflow.com/questions/15447558/can-not-include-ttf-font-into-project 本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1438678,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册
修改密码
忘记密码