首页 文章 精选 留言 我的

精选列表

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

【iOS开发必备指南合集二】申请IDP/真机调试/GameCenter 指南/OpenFeint指南

OpenFeint 是很多iPhone游戏开发者都要用到的社区功能;一、openfeint中的LeaderBoards 和Achievement的一点体会 1.数据提交的格式 最近想向自己 的游戏中添加点openfeint功能,使用的时候发现,openfeint的功能虽然比较多,也比较强大,但是,有些地方还是不太如人意。我游戏中的分 数有一项是float型的数据,可是当我提交的时候,发现openfeint的在线排名只支持整数形式的数据,改变了官方的api提交之后,服务器那边仍 自动转换成了整型的数据。我在论坛上求证了一下,虽然没有结论,但我认为openfeint高分排行榜仅支持整型的数据。 2.数据提交的方法 [OFHighScoreService setHighScore:你提交的分数forLeaderboard:@"分数项的ID" onSuccess:OFDelegate() onFailure:OFDelegate()]; //提交高分,如果函数无效,请引入#import "OFHighScoreService.h" 通过上面那个函数,就可以向服务器提 交数据,其中你要提交的分数,无论是什么类型,最后都会转换成整型的数据,可以参见上一条信息。而分数项ID,则是你在申请LaderBoards的时候 openfeint分配给你的一个数字。后面两个参数,应该不需要改变,我没有尝试过做其他的动作,有兴趣和想法的朋友,可以尝试象@selector那 样使用它。 [OFAchievementService unlockAchievement: @"成就ID" onSuccess: OFDelegate() onFailure: OFDelegate()];//解锁成就,如果函数无效,请引入#import "OFAchievementService.h" 这个函数的功能是解锁成就,当你在游戏过程中达到某一个要求时,就可以解锁你在openfeint上预设的成就。 例如: 生存游戏中:if (生存时间 > 100s ){ 调用上面的函数解锁你自己预设的成就; } jump游戏中:if ( 高度 > 10000m ){ 调用上面的函数解锁你自己预设的成就; } 3.网络对分数提交的影响 网络畅通的情况下,调用上述函数提交分数(最高分数被刷新时) 可以成功,并且解锁成就并不会反复出现解锁提示。好吧,既然这个可以完成我们的要求,那么这里就不是重点。 网络不通的情况下,就会出现一点问题: 在阐明问 题之前,我想先说一下我对openfeint的数据存储的理解或者说感觉。使用penfeint的时候,在documents目录下会生成两个文件,一个 FakeKeyChain.plist,据我观察,这里面存放的就是我们在openfeint里为这个游戏申请的Product Key和Product Secret,而且Secret经过了加密处理。另一个文件则是feint-offline, 这个文件是无法打开的,在windows用记事本打开也是一堆乱码,也许有其他的办法,不过我没有找到。我对于这个文件的用途猜测是,这个文件用来存档玩 家的一些信息,比如玩家名和分数等,这个文件我暂时叫它为“本地隐 藏信息表”吧。 问题来鸟,在没有网络的情况下,取得了一个分数,然后第一次调用分数提交函数,会提示你得 到了一个高分,存储在本地(我感觉就在本地隐藏信息表中), 问题出现了!当你这时连接网络,分数并不会自动提交,而你自己手动提交(比如点击一个按钮,按钮的功能是提交最高分数)也没有任何的效果。 而 在官方文档中有这样一Q&A: Q:what happens to a high score when a player is offline? A:as os openfeint 2.1 high scoreare queued for submission when the player is offline and submitted when next he's online again. Q:if a user says no to using openfeint the first time,is there a way that user can change his or her mind to allow openfeint in the future? A:when you deny openfeint it will prompt you to approve/deny again when you open the dashboard([OpenFeint launchDashboard]).it will not prompt you on the nextappbootup,or submitting any requests.only when you open the dashboard. 也就是说,提交失败,于是我做了个试验,在有网络的情况下,提交一个分数100,只显示 一次,第二次提交100时,没有提示。然后提交101,有提示,第二次提交101,没有提示。说明了本地隐藏信息表中还存储了一个最高分数的提交次数和提交许可,使用一次提交分数的函数,这些内 容就会改变,只有新提交的分数比原来存储的分数大时,本地隐藏信息 表才会允许你向openfeint正式提交,否则,无效,感觉上和retain与release有点像。也就时说,最高的分数在提交的时 候,没有网络,就等于失败,这里应该算是openfeint的一个小bug吧。也是我遇到问题的所在,没找到什么解决办法,大伙有经验的可以提出来。 用 个简单的图来形容下吧。 无网络->得到新高分->存储在本地->联网后->不自动上传最高分。 顺 便说下成就的提交,没有网络,不可解锁成就,也没有存在本地的提示,联网后,同样也不自动解锁,只有再一次达到条件(方才例子中的if成立)时,才会再次 解锁。 以上,是我的部分openfeint基础使用的经验,也许是我的方法不正确,也许有别的解决办法,我能提供给大家的帮助, 先这么多了。二、 openfeint的设置(2.4.8版) 以下步骤是假设你从没安装过openfeint,如果有,请将以 前老版本的openfeint从机器中删除,并从project中删除所有与openfeint有关的东西,然后,你可以按下面的步骤来做了。 1. 从官网下载一个最新版本的没有解压的openfeint SDK。 2.将openfeint文件夹拖入你的project中。 3. 设置info a.打开project的info,选中build栏,将configuration设置成All configuration b.将Other Linker Flags一项的值设置成 -Objc区分大小写 c.将Call C++ Default Ctors/Dtors in Objective-c项的选成yes(这一步我没有设置,不知道是什么意思,英文原文如下:Ensure 'Call C++ Default Ctors/Dtors in Objective-c' is checked under the 'GCC 4.2 - Code Generation' section) d.设置一个默认的值GCC_OBJC_CALL_CXX_CDTORS 为 YES(这一步我也没有设置) 4.引入frameworks 需要引入的frameworks 有,Foundation,UIKit,CoreGraphics,QuartCore,Security,SystemConfiguration,libsql3.0 dylie,libz.1.2.3.dylib这些是官方给出需要引入的frameworks,根据帮我搭建工程的前辈说,必须要引入 CoreLocation.frameworkCFNetwork.framework MapKit.framework 5.必须在你的 .pch 文件中引入#import “OpenFeintPrefix.pch” 6.将你所有使用openfeint功能的函数改为.mm文件 我能想到的就这些了,还有什么问题,大家可以互相讨论。三、openfeint的注册 openfeint的注册并不难,能看懂 文档的水平基本就可以了,也可以配合翻译软件来弄。 1.打开官网http://www.openfeint.com 2.选右上角的 Developers一项,跳转到的新界面。 3.点击本页面的右上角的login会进入登陆界面,选择右上角的 sign up进行一个简单注册,本页右下角有一个教学的视频,告诉你如何使用openfeint的基础功能。 4.简单注册界面,填写完成后跳转到一个新的界面,点击Dive in 进入你自己的openfeint里。 5.进入自己的openfeint了,需要进行一个prepare for submission的申请,这个可以让你的openfeint有效,否则,你只能使用test user 进行测试。在App Home中,可以看到自己的client Id 这个是用来提问用的,以及最重要的Product Key和Product Secret,这两项是用来识别你的程序独有的openfeint的。 6.还需要一个你注册时使用的邮箱认证。进入自己的邮箱就能看到 了。 7.想通过openfeint的审核,还需要完善一个ipurchase的填写,在basic features/iPurchase里面填写,*项必须要有内容。 8.完成上面这些,你就可以设置自己的LaderBoards和 Achievement了,还有更多的challenge等。 ------以上为Himi的经验总结以及参考资料,希望对大家有帮助; 本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/699765,如需转载请自行联系原作者

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

iOS 处理服务器返回的数据字符串为之 Null

昨晚遇到了无法将服务器返回的数据写在 plist 文件中的问题。我尝试打印了服务器返回的数据,发现好几项值为 Null !!! Null !!! 经过各种折腾之后 GitHub 上找到了爽快的解决处理服务器返回的数据字符串为 <null> 的方法。 使用方法: 下载 SetNulllWithStr 添加到项目 导入头文件 处理服务器返回的数据 NSDictionary *dic = (NSDictionary*)responseObject; NSDictionary *newDic = [NSDictionary changeType:dic]; 处理服务器返回的数据 responseObject 为服务器返回的数据

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

iOS: xcode打包上传iTunes失败,iTunes Store operation failed,this action can n...

通过xcode点击“upload to app store”上传到itunes,结果一直提示“itunes store operation failed” 原因:网速的问题,我之前也遇到过,网速好的时候上传就没有问题了 解决办法: 1.多试几次,实在不行就换个网络环境 2.打个上传的包用application loader上传 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5659585.html ,如需转载请自行联系原作者

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

iOS:UICollectionView流式布局及其在该布局上的扩展的线式布局

UICollectionViewFlowLayout是苹果公司做好的一种单元格布局方式,它约束item的排列规则是:从左到右依次排列,如果右边不够放下,就换一行重复上面的方式排放,,,,, 常用的流式布局UICollectionViewFlowLayout的属性 @property(nonatomic)CGFloatminimumLineSpacing; //每一个item之间最小的行间距 @property(nonatomic)CGFloatminimumInteritemSpacing;//每一个item之间最小的列间距 @property(nonatomic)CGSizeitemSize; //每一个item的大小 @property(nonatomic)CGSizeestimatedItemSize; //每一个item的预测大小 @property(nonatomic)UICollectionViewScrollDirectionscrollDirection;// 集合视图的滚动方向,默认垂直 @property(nonatomic)CGSize headerReferenceSize; //表头视图大小 @property(nonatomic)CGSizefooterReferenceSize; //表尾视图大小 @property(nonatomic)UIEdgeInsetssectionInset; //和集合视图上下左右四边的间距 使用流式布局很简单,不需要我们做任何的操作,只需要创建它的实例,然后把它放入创建的集合视图中即可。然而,流式布局看起来比较的单一,没有很炫酷的效果。基于此,我们可以在流式布局的基础上进行一些布局的扩展,比如线式布局等。。。 下面就做一个流式布局和线式布局的切换,点击时,可以自由切换,使集合视图的item排列呈现出不同的效果,举例如下: 1、首先创建一个自定义的单元格类,并附带创建一个.xib文件,在.xib文件中设置一个UIImageView,将它IBOutLet到对应的类中 2、准备一些图片素材 3、在ImagesCell类中 .h #import <UIKit/UIKit.h> @interface ImagesCell : UICollectionViewCell @property (strong,nonatomic)UIImage *image; @end .m #import "ImagesCell.h" @interface ImagesCell() @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ImagesCell - (void)awakeFromNib { //设置图像视图图层的属性 self.imageView.layer.borderWidth = 3; self.imageView.layer.borderColor = [[UIColor redColor]CGColor]; self.imageView.layer.cornerRadius = 5; self.imageView.clipsToBounds = YES; //切割边角 } -(void)setImage:(UIImage *)image { _image = image; //显示图片 [_imageView setImage:_image]; } @end 3.在控制器类ViewController类中 #import "ViewController.h" #import "ImagesCell.h" #import "CustomLineLayout.h" @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate> @property (strong,nonatomic)UICollectionView *collectionView; @property (strong,nonatomic)NSMutableArray *images; @end @implementation ViewController static NSString *const reuseIndentifier = @"image"; //懒加载 -(NSMutableArray *)images { if (!_images) { _images = [NSMutableArray array]; for (int i=1; i<=25; i++) { [_images addObject:[NSString stringWithFormat:@"clothes%d",i]]; } } return _images; } - (void)viewDidLoad { [super viewDidLoad]; //创建集合视图 CGFloat width = self.view.frame.size.width; CGRect rect = CGRectMake(0, 100, width, 200); self.collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:[[CustomLineLayout alloc]init]]; //设置数据源和代理 self.collectionView.dataSource = self; self.collectionView.delegate = self; //注册单元格 [self.collectionView registerNib:[UINib nibWithNibName:@"ImagesCell" bundle:nil] forCellWithReuseIdentifier:reuseIndentifier]; //添加视图 [self.view addSubview:self.collectionView]; //UICollectionViewLayout:最根本的布局,自定义布局时,完全需要自己重新去写需要的布局 //UICollectionViewFlowLayout :流水布局,自定义布局时,有时可以在它的布局基础上再进行扩展布局 } //切换布局方式 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([self.collectionView.collectionViewLayout isKindOfClass:[CustomLineLayout class]]) { [self.collectionView setCollectionViewLayout:[[UICollectionViewFlowLayout alloc]init] animated:YES]; } else { [self.collectionView setCollectionViewLayout:[[CustomLineLayout alloc]init] animated:YES]; } } #pragma mark - <UICollectionDataSourceDelegate> //返回组数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } //返回个数 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.images.count; } //显示conllectionView的单元格 -(ImagesCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { //设置重用单元格 ImagesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIndentifier forIndexPath:indexPath]; //设置cell的内容 cell.image = [UIImage imageNamed:[self.images objectAtIndex:indexPath.item]]; return cell; } //选中item时删除它 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { //1.先删除掉对应的模型数据 [self.images removeObjectAtIndex:indexPath.item]; //2.删除item(刷新UI) [self.collectionView deleteItemsAtIndexPaths:@[indexPath]]; } @end 4、自定义线式布局,它继承于流式布局,即 在.m文件中设置每一个item的布局属性如下: #import "CustomLineLayout.h" //设置item的固定的宽和高 static const CGFloat itemWH = 100; //设置缩放时的有效距离 static const CGFloat activeDistance = 150; //设置缩放因数,值越大,缩放效果越明显 static const CGFloat scaleFactor = 0.6; @implementation CustomLineLayout //UICollectionViewLayoutAttributes:很重要,布局属性设置 //每一个cell(item)都有自己的UICollectionViewLayoutAttributes //每一个indexPath都有自己的UICollectionViewLayoutAttributes -(instancetype)init{ if (self = [super init]){ } return self; } //每一次重新布局前,都会准备布局(苹果官方推荐使用该方法进行一些初始化) -(void)prepareLayout { [super prepareLayout]; //初始化,设置默认的item属性 self.itemSize = CGSizeMake(itemWH, itemWH); self.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.minimumLineSpacing = itemWH * scaleFactor; //将第一个和最后一个item始终显示在中间,即分别将它们设置到组头和组尾的距离 CGFloat inset = (self.collectionView.frame.size.width - itemWH) / 2; self.sectionInset = UIEdgeInsetsMake(0, inset, 0, inset); } //是否要重新刷新布局(只要显示的item边界发生改变就重新布局) //只要每一次重新布局内部就会调用下面的layoutAttributesForElementsInRect:获取所有cell(item)的属性 -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } //用来设置colectionView停止滚动时的那一刻位置,内部会自动调用 #pragma targetContentOffset : 原本colectionView停止滚动时的那一刻位置 #pragma velocity : 滚动的速率,根据正负可以判断滚动方向是向左还是向右 -(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { //1.计算colectionView最终停留的位置 CGRect lastRect; lastRect.origin = proposedContentOffset; lastRect.size = self.collectionView.frame.size; //2.取出这个范围内的所有item的属性 NSArray *array = [self layoutAttributesForElementsInRect:lastRect]; //3.计算最终屏幕的中心x CGFloat centerX = proposedContentOffset.x+ self.collectionView.frame.size.width/2; //4.遍历所有的属性,通过计算item与最终屏幕中心的最小距离,然后将item移动屏幕的中心位置 CGFloat adjustOffsetX = MAXFLOAT; for (UICollectionViewLayoutAttributes *attris in array) { if (ABS(attris.center.x - centerX) < ABS(adjustOffsetX)) { adjustOffsetX = attris.center.x - centerX; } } //5.返回要移动到中心的item的位置 return CGPointMake(proposedContentOffset.x + adjustOffsetX , proposedContentOffset.y); } //返回需要重新布局的所有item属性 -(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { //0.计算可见的矩形框属性 CGRect visiableRect; visiableRect.size = self.collectionView.frame.size; visiableRect.origin = self.collectionView.contentOffset; //1.取出默认的cell的UICollectionViewLayoutAttributes NSArray *array = [super layoutAttributesForElementsInRect:rect]; //计算屏幕最中心的x(滚出去的所有的item的偏移 + collectionView宽度的一半) CGFloat centerX = self.collectionView.contentOffset.x + self.collectionView.frame.size.width/2; //2.遍历所有的布局属性 for(UICollectionViewLayoutAttributes *attrs in array) { //如果遍历的item和可见的矩形框的frame不相交,即不e是可见的,就直接跳过,只对可见的item进行放缩 if (!CGRectIntersectsRect(visiableRect, attrs.frame)) continue; //每一个item的中心x CGFloat itemCenterX = attrs.center.x; //差距越大,缩放比例越小 //计算每一个item的中心x和屏幕最中心x的绝对值距离,然后可以计算出缩放比例,即 //<1>计算间距/屏幕一半时的比例,得出的数一定<1 //CGFloat ratio = ABS(itemCenterX - centerX) / (self.collectionView.frame.size.width/2); //CGFloat ratio = ABS(itemCenterX - centerX) / 150; //<2>实现放大 //CGFloat scale = 1 + (1 - ratio); //attrs.transform3D = CATransform3DMakeScale(scale, scale, 1.0); //attrs.transform = CGAffineTransformMakeScale(scale, scale); //当item的中心x距离屏幕的中心x距离在有效距离150以内时,item才开始放大 if (ABS(itemCenterX - centerX) <= activeDistance) { //CGFloat ratio = ABS(itemCenterX - centerX) / (self.collectionView.frame.size.width/2); CGFloat ratio = ABS(itemCenterX - centerX) / activeDistance; //<2>实现放大 CGFloat scale = 1 + scaleFactor*(1 - ratio); attrs.transform3D = CATransform3DMakeScale(scale, scale, 1.0); //attrs.transform = CGAffineTransformMakeScale(scale, scale); } else { attrs.transform = CGAffineTransformMakeScale(1, 1); } } return array; } @end 演示结果如下: 流式布局: 切换为线式布局: 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4985527.html ,如需转载请自行联系原作者

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

iOS:第三方框架MJPhotoBrowser图片浏览器的使用

介绍:MJPhotoBrowser这个第三方库是MJ老师封装的一套用来浏览图片的浏览器,可是是本地图片、网络图片、gif图片等,其也依赖了SDWebImage、SVProgressHUD、YLGIFImage这些三方库,因为高度封装,所以集成起来比较简单,貌似已经停止更新并卸下了。下面看一些几个重要的类: MJPhotoBrowser框架:http://files.cnblogs.com/files/XYQ-208910/MJPhotoBrowser.zip 图片模型类MJPhoto MJPhoto.h #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import <QuartzCore/QuartzCore.h> #import <YLGIFImage/YLImageView.h> #import <YLGIFImage/YLGIFImage.h> #import <SDWebImage/UIImageView+WebCache.h> #import <SVProgressHUD/SVProgressHUD.h> @interface MJPhoto : NSObject @property (nonatomic, strong) NSURL *url; //图片链接 @property (nonatomic, strong) UIImage *image; // 完整的图片 @property (nonatomic, strong) UIImageView *srcImageView; // 来源view @property (nonatomic, strong, readonly) UIImage *placeholder; //占位图片 @property (nonatomic, strong, readonly) UIImage *capture; //截图 // 是否已经保存到相册 @property (nonatomic, assign) BOOL save; @property (nonatomic, assign) int index; // 索引 @end MJPhoto.m #import "MJPhoto.h" @implementation MJPhoto #pragma mark - 截图 - (UIImage *)capture:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } - (void)setSrcImageView:(UIImageView *)srcImageView { _srcImageView = srcImageView; _placeholder = srcImageView.image; if (srcImageView.clipsToBounds) { _capture = [self capture:srcImageView]; } } @end 预览器类MJPhotoBrowser MJPhotoBrowser.h #import "MJPhoto.h" @protocol MJPhotoBrowserDelegate; @interface MJPhotoBrowser : NSObject <UIScrollViewDelegate> // 所有的图片对象 @property (nonatomic, strong) NSArray *photos; // 当前展示的图片索引 @property (nonatomic, assign) NSUInteger currentPhotoIndex; // 保存按钮 @property (nonatomic, assign) NSUInteger showSaveBtn; // 显示图片 - (void)show; @end 集成过来使用的方法: 1.本地图片 -(void)addImage:(UIImage *)image { [self.photos addObject:image]; UIImageView *imageView = [[UIImageView alloc]initWithImage:image]; imageView.contentMode = UIViewContentModeScaleToFill; imageView.userInteractionEnabled = YES; [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTap:)]]; [self addSubview:imageView]; } //点击预览图片 - (void)photoTap:(UITapGestureRecognizer *)recognizer { //1.创建图片浏览器 NSMutableArray *kjphotos = [NSMutableArray array]; MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init]; //2.告诉图片浏览器显示所有的图片 for (int i = 0 ; i < self.photos.count; i++) { //传递数据给浏览器 MJPhoto *photo = [[MJPhoto alloc] init]; photo.image = self.photos[i]; photo.srcImageView = self.subviews[i]; //设置来源哪一个UIImageView [kjphotos addObject:photo]; } brower.photos = kjphotos; //3.设置默认显示的图片索引 brower.currentPhotoIndex = recognizer.view.tag; //4.显示浏览器 [brower show]; } 2.网络图片 //监听图片的点击 - (void)tapPhoto:(UITapGestureRecognizer *)recognizer { //1.创建图片浏览器 MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init]; //2.告诉图片浏览器显示所有的图片 NSMutableArray *photos = [NSMutableArray array]; for (int i = 0 ; i < self.photos.count; i++) { Photo *pic = self.photos[i]; //传递数据给浏览器 MJPhoto *photo = [[MJPhoto alloc] init]; photo.url = [NSURL URLWithString:pic.bmiddle_pic]; photo.srcImageView = self.subviews[i]; //设置来源哪一个UIImageView [photos addObject:photo]; } brower.photos = photos; //3.设置默认显示的图片索引 brower.currentPhotoIndex = recognizer.view.tag; //4.显示浏览器 [brower show]; } 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5283103.html ,如需转载请自行联系原作者

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

iOS:使用block代码块实现事件处理过程中的回调

block是什么,这里就不多加强调了,它的优点: 第一:执行效率高,速度快 第二:使用起来比代理简单,省却不少代码,增强代码美感 有一些小的知识点要强调一下: 第一点:它类似于一个匿名函数,也跟java中的匿名内部类相似,但是,记住,它是一种数据类型,因为它内部是一个结构体,有方法有属性,所以它具有对象的特征; 第二点:在类中声明block为属性时,如果使用assgin修饰,那么它被放到了栈中,方法已过就会被销毁,所以,尽量使用copy作为修饰词,这样一来block就被存放到了堆中,生命周期就会延长,保证block不会被立即销毁; 第三点:要防止循环引用,造成线程死锁,所以,有时需要加上__weak修饰。 第四点:为什么多用copy修饰,而不用strong修饰,这是MRC时期遗留下来的写法习惯,其实用strong也可以修饰,但是尽量用copy。 block既可以作为属性,单独赋值然后回调;也可以作为一个方法的参数使用,再进行回调,举例如下,直接代码演示: 我的例子过程大概是:首先用户浏览题库时,点击收藏按钮,弹出收藏界面,用户选择完某一个文件夹后,隐藏收藏界面,提示收藏成功! 例子一:block使用属性单独赋值后回调 这是收藏界面类的回调代码 #import <UIKit/UIKit.h> //声明block,用作选择收藏文件后的回调处理 typedef void (^CollectionBlock)(); //选择文件夹 typedef void (^CompleteHandleBlock)(); //选完之后的处理 @interface KJCollectionView : UITableView @property (copy,nonatomic)CollectionBlock collectionBlock; @property (copy,nonatomic)CompleteHandleBlock completeBlock; -(instancetype)initWithFrame:(CGRect)frame withFiles:(NSArray *)files; @end #import "KJCollectionView.h" #import "KJMyQueBankFile.h" @interface KJCollectionView()<UITableViewDataSource,UITableViewDelegate> @property (strong,nonatomic)NSArray *allFiles; @end @implementation KJCollectionView -(instancetype)initWithFrame:(CGRect)frame withFiles:(NSArray *)files { self = [super initWithFrame:frame]; if (self) { self.dataSource = self; self.delegate = self; self.tableHeaderView = [UILabel createLabel:CGRectMake(0, 0, SCREEN_WIDTH, 50) title:@"请选择收藏位置" titleColor:HMColor(13, 195, 176) alignmentType:NSTextAlignmentCenter size:16]; self.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero]; self.allFiles = [NSArray arrayWithArray:files]; MoveUnderLine(self); } return self; } #pragma mark - block回调,为block属性赋值 -(void)setCollectionBlock:(CollectionBlock)collectionBlock{ if (_collectionBlock != collectionBlock) { _collectionBlock = collectionBlock; } } -(void)setCompleteBlock:(CompleteHandleBlock)completeBlock{ if (_completeBlock != completeBlock) { _completeBlock = completeBlock; } } #pragma mark - UITableViewDataSource #pragma mark - Required -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.allFiles.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuseIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (!cell) { cell = [[UITableViewCell alloc]init]; } cell.accessoryView = [UIButton createButton:CGRectMake(0, 0, 30, 30) imageName:@"default_true" target:self Done:@selector(chooseFileDone:)]; cell.imageView.image = [UIImage imageNamed:[self.allFiles[indexPath.row] file_image]]; cell.textLabel.text = [self.allFiles[indexPath.row] file_Name]; return cell; } #pragma mark - Optional - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50; } #pragma mark - UITableViewDelegate #pragma mark - Optional - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryView = [UIButton createButton:CGRectMake(0, 0, 30, 30) imageName:@"selected_ture" target:nil Done:nil]; //回调处理 _collectionBlock(); _completeBlock(); } #pragma mark - 按钮选择文件夹 -(void)chooseFileDone:(UIButton *)btn{ UITableViewCell *cell = (UITableViewCell *)btn.superview; cell.accessoryView = [UIButton createButton:CGRectMake(0, 0, 30, 30) imageName:@"selected_ture" target:nil Done:nil]; //回调处理 _collectionBlock(); _completeBlock(); } @end 这是在题库类中给block传递回调处理的代码:直接通过属性赋值 #pragma mark - 点击收藏按钮时的代理方法 /** * 收藏作业 * @param indexPath 当前cell中选择按钮选中后的cell * @param btn 收藏按钮 * @param collectionTag 当前cell中收藏按钮的tag */ -(void)finishedClickedCollectionFileIndexPath:(NSIndexPath *)indexPath CollectionBtn:(UIButton *)btn{ //创建收藏文件夹视图和蒙版视图 self.collectionView = [[KJCollectionView alloc]initWithFrame:CGRectMake(0, 0, 260, 200) withFiles:self.filesArrayM]; self.collectionView.layer.cornerRadius = 3.0; self.collectionView.layer.masksToBounds = YES; self.collectionView.center = [UIApplication sharedApplication].keyWindow.center; self.coverView = [[UIView alloc]initWithFrame:self.view.bounds]; self.coverView.backgroundColor = HMColorRGBA(51, 51, 51, 0.3); [self.view addSubview:self.collectionView]; [self.view addSubview:self.coverView]; [self.view insertSubview:self.collectionView aboveSubview:self.coverView]; __weak typeof(self) weakSelf = self; //为选择文件的block赋值 self.collectionView.collectionBlock = ^(){ //取出收藏字典 KJChooseHomeworkDicM *checkBtnDicManager = [KJChooseHomeworkDicM sharedCheckBtnDicMManager]; [checkBtnDicManager.collectionDicM setObject:indexPath forKey:indexPath]; [btn setSelected:YES]; //获取作业模型 KJLog(@"clickedCollection:%@",[weakSelf.homeworkCellFrames[indexPath.row] defaultFH]); }; //为选完文件后的block赋值 self.collectionView.completeBlock = ^(){ [MBProgressHUD showSuccess:@"收藏成功"]; //移除收藏文件夹视图和蒙版视图 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf.collectionView removeFromSuperview]; [weakSelf.coverView removeFromSuperview]; [weakSelf setCollectionView:nil]; [weakSelf setCoverView:nil]; }); }; } 例子二:block作为方法的参数赋值后再进行回调 这是收藏界面类的回调代码 #import <UIKit/UIKit.h> //声明block,用作选择收藏文件后的回调处理 typedef void (^CollectionBlock)(); //选择文件夹 typedef void (^CompleteHandleBlock)(); //选完之后的处理 @interface KJCollectionView : UITableView @property (copy,nonatomic)CollectionBlock collectionBlock; @property (copy,nonatomic)CompleteHandleBlock completeBlock; -(instancetype)initWithFrame:(CGRect)frame withFiles:(NSArray *)files; //声明一个方法 -(void)chooseFile:(CollectionBlock )collectionBlock compeleteChooseBlock:(CompleteHandleBlock)completeBlock; @end // Created by mac on 16/5/20. // Copyright © 2016年 mac. All rights reserved. #import "KJCollectionView.h" #import "KJMyQueBankFile.h" @interface KJCollectionView()<UITableViewDataSource,UITableViewDelegate> @property (strong,nonatomic)NSArray *allFiles; @end @implementation KJCollectionView -(instancetype)initWithFrame:(CGRect)frame withFiles:(NSArray *)files { self = [super initWithFrame:frame]; if (self) { self.dataSource = self; self.delegate = self; self.tableHeaderView = [UILabel createLabel:CGRectMake(0, 0, SCREEN_WIDTH, 50) title:@"请选择收藏位置" titleColor:HMColor(13, 195, 176) alignmentType:NSTextAlignmentCenter size:16]; self.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero]; self.allFiles = [NSArray arrayWithArray:files]; MoveUnderLine(self); } return self; } #pragma mark - block回调,通过方法为block属性赋值 -(void)chooseFile:(CollectionBlock )collectionBlock compeleteChooseBlock:(CompleteHandleBlock)completeBlock{ if (_collectionBlock != collectionBlock) { _collectionBlock = collectionBlock; } if (_completeBlock != completeBlock) { _completeBlock = completeBlock; } } #pragma mark - UITableViewDataSource #pragma mark - Required -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.allFiles.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuseIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; if (!cell) { cell = [[UITableViewCell alloc]init]; } cell.accessoryView = [UIButton createButton:CGRectMake(0, 0, 30, 30) imageName:@"default_true" target:self Done:@selector(chooseFileDone:)]; cell.imageView.image = [UIImage imageNamed:[self.allFiles[indexPath.row] file_image]]; cell.textLabel.text = [self.allFiles[indexPath.row] file_Name]; return cell; } #pragma mark - Optional - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 50; } #pragma mark - UITableViewDelegate #pragma mark - Optional - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryView = [UIButton createButton:CGRectMake(0, 0, 30, 30) imageName:@"selected_ture" target:nil Done:nil]; //回调处理 _collectionBlock(); _completeBlock(); } #pragma mark - 按钮选择文件夹 -(void)chooseFileDone:(UIButton *)btn{ UITableViewCell *cell = (UITableViewCell *)btn.superview; cell.accessoryView = [UIButton createButton:CGRectMake(0, 0, 30, 30) imageName:@"selected_ture" target:nil Done:nil]; //回调处理 _collectionBlock(); _completeBlock(); } @end 这是在题库类中给block传递回调处理的代码:直接将要赋值的block写入到方法中 #pragma mark - 点击收藏按钮时的代理方法 /** * 收藏作业 * @param indexPath 当前cell中选择按钮选中后的cell * @param btn 收藏按钮 * @param collectionTag 当前cell中收藏按钮的tag */ -(void)finishedClickedCollectionFileIndexPath:(NSIndexPath *)indexPath CollectionBtn:(UIButton *)btn{ //创建收藏文件夹视图和蒙版视图 self.collectionView = [[KJCollectionView alloc]initWithFrame:CGRectMake(0, 0, 260, 200) withFiles:self.filesArrayM]; self.collectionView.layer.cornerRadius = 3.0; self.collectionView.layer.masksToBounds = YES; self.collectionView.center = [UIApplication sharedApplication].keyWindow.center; self.coverView = [[UIView alloc]initWithFrame:self.view.bounds]; self.coverView.backgroundColor = HMColorRGBA(51, 51, 51, 0.3); [self.view addSubview:self.collectionView]; [self.view addSubview:self.coverView]; [self.view insertSubview:self.collectionView aboveSubview:self.coverView]; __weak typeof(self) weakSelf = self; //把block放入方法中 [self.collectionView chooseFile:^{ //取出收藏字典 KJChooseHomeworkDicM *checkBtnDicManager = [KJChooseHomeworkDicM sharedCheckBtnDicMManager]; [checkBtnDicManager.collectionDicM setObject:indexPath forKey:indexPath]; [btn setSelected:YES]; [MBProgressHUD showSuccess:@"收藏成功"]; //获取作业模型 KJLog(@"clickedCollection:%@",[weakSelf.homeworkCellFrames[indexPath.row] defaultFH]); } compeleteChooseBlock:^{ //移除收藏文件夹视图和蒙版视图 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [MBProgressHUD hideHUD]; [weakSelf.collectionView removeFromSuperview]; [weakSelf.coverView removeFromSuperview]; [weakSelf setCollectionView:nil]; [weakSelf setCoverView:nil]; }); }]; } 上面两种演示截图达到的效果是一样的: 点击题库中的收藏 选择文件夹收藏 显示收藏成功 移除文件夹视图 本人原创,转载须注明出处,谢谢! 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5512618.html ,如需转载请自行联系原作者

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

iOS:百度长语音识别具体的封装:识别、播放、进度刷新

一、介绍 以前做过讯飞语音识别,比较简单,识别率很不错,但是它的识别时间是有限制的,最多60秒。可是有的时候我们需要更长的识别时间,例如朗诵古诗等功能。当然讯飞语音也是可以通过曲线救国来实现,就是每达到60秒时识别停止就立即重新开启,每次结束拼接录音。这么做,显然是麻烦的,百度语音解决了这个问题,它最近上线了长语音识别技术,可以不受时间限制,还是非常棒的。这次,我就专门抽成一个工具类使用,包括语音识别,录音拼接,录音播放、进度刷新,具体如何集成SDK看官方文档,我就不废话了,直接介绍如何使用我的这个工具类。 二、依赖 因为语音录制格式是pcm格式的,我使用lame静态库将其转成了mp3格式。 下载路径参考我的博客,有具体的介绍:http://www.cnblogs.com/XYQ-208910/p/7650759.html 三、代码 SJVoiceTransform.h View Code SJVoiceTransform.m View Code BDHelper.h View Code BDHelper.m View Code 四、注意 百度语音SDK只支持armv6、armv7的真机架构,不支持x86_64模拟器架构。 五、如何在模拟器下开发 办法: 1、首先将涉及到百度语音的代码全部采用宏定义注释掉,如: #if !TARGET_IPHONE_SIMULATOR // 语音相关调用 // self.asrEventManager = [BDSEventManager createEventManagerWithName:BDS_ASR_NAME]; // 其他调用 #endif 2、(重点要处理的地方)切换到模拟时,将libBaiduSpeechSDK.a静态包从Linked Frameworks and Librarise删掉(切换到真机时,再将libBaiduSpeechSDK.a导入进来就行) 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/8185226.html ,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

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

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

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