首页 文章 精选 留言 我的

精选列表

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

iOS开发-简单抽奖

路过商场,看过抽奖感觉挺有意思的,商场进行抽奖活动,三个奖项,一等奖的概率1/10,二等奖的概率的3/10,三等奖的概率是6/10,具体奖品我没仔细看,回来随便练手了一下,思考了一下,奖品分为10份,生成一个随机数,如果0-5则是三等奖,6-8是二等奖,9是一等奖,简单实现如下: 1 2 3 4 5 6 7 8 int randomNumber=arc4random()%10; if (randomNumber>=0&&randomNumber<=5) { [ self alert:@ "恭喜你获得三等奖手机充值卡999元" ]; } else if (randomNumber>=6&&randomNumber<=8){ [ self alert:@ "恭喜你获得二等奖iPad3" ]; } else { [ self alert:@ "恭喜你获得一等奖iPhone6" ]; } 简单提示: 1 2 3 4 5 -( void )alert:( NSString *)message{ // http://www.cnblogs.com/xiaofeixiang UIAlertView *alterView=[[UIAlertView alloc]initWithTitle:@ "抽奖结果" message:message delegate: self cancelButtonTitle:@ "确定" otherButtonTitles: nil ]; [alterView show]; } 效果如下: 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4524023.html,如需转载请自行联系原作者

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

iOS开发-UIButton浅谈

UIButton算是最基本的一个控件了,不过有的时候用法挺多关于UIButton文字的位置,字体大小,字体的颜色 1.设置UIButton字体大小,尤其注意不要使用直接调用setFont: 1 [ self .playButton.titleLabel setFont:[UIFont systemFontOfSize:14]]; 2.UIButton默认背景是白色的,如果文字默认颜色是白色的,是看不到文字的,设置标题颜色: 1 [ self .playButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 3.设置颜色之后很有可能标题没法显示,检查一下是不是通过titleLabel设置的,应该是直接设置: 1 [ self .playButton setTitle:@ "FlyElephant" forState:UIControlStateNormal]; 4.设置文字居中,最容易通过title设置NSTextAlignment,结果发现不尽人意,这个时候可以通过contentHorizontalAlignment设置: 1 self .playButton.contentHorizontalAlignment= UIControlContentHorizontalAlignmentLeft; 垂直方向的设置和水平方向差不多: 1 self .playButton.contentVerticalAlignment=UIControlContentVerticalAlignmentBottom; 5.设置居左之后可能发现太过于居左,可以通过setContentEdgeInsets设置: 1 [ self .playButton setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)]; 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4541293.html,如需转载请自行联系原作者

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

iOS开发-UIScreenEdgePanGestureRecognizer实战

UIScreenEdgePanGestureRecognizer名字很长,而且关于其文档也是少的的可怜,苹果官方给的唯一的一个属性是edges,文档中的解释是这样的: 1 A UIScreenEdgePanGestureRecognizer looks for panning (dragging) gestures that start near an edge of the screen. The system uses screen edge gestures in some cases to initiate view controller transitions. You can use this class to replicate the same gesture behavior for your own actions. 大概的意思就是UIScreenEdgePanGestureRecognizer跟pan(平移)手势差不多,需要从边缘进行拖动,在控制器转换的时候是有用的,看文档的话我们会发现UIScreenEdgePanGestureRecognizer是UIPanGestureRecognizer的子类,理解会更方便一点。 UIPanGestureRecognizer铺垫 先简单的看下需要实现的视图控制器的效果: 稍微回顾一下UIPanGestureRecognizer,第一个红色的视图我们通过Pan手势进行操作: 1 2 3 4 5 6 7 8 9 self.panView=[[UIView alloc]initWithFrame:CGRectMake(0, 200, CGRectGetWidth(self.view.bounds), 100)]; [self.panView setBackgroundColor:[UIColor redColor]]; self.panLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, 150, 40)]; [self.panLabel setText: @"博客园-FlyElephant" ]; [self.panLabel setFont:[UIFont systemFontOfSize:14]]; [self.panView addSubview:self.panLabel]; [self.view addSubview:self.panView]; UIPanGestureRecognizer *pangestureRecognizer=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)]; [self.panView addGestureRecognizer:pangestureRecognizer]; 手势事件: 1 2 3 4 -( void )panGesture:(UIPanGestureRecognizer *)gesture{ CGPoint translation = [gesture translationInView:gesture.view]; NSLog( @"%@" ,[NSString stringWithFormat: @"(%0.0f, %0.0f)" , translation.x, translation.y]); } 手势向左滑动的panView的变化: UIScreenEdgePanGestureRecognizer实战 第二个视图我们可以通过UIScreenEdgePanGestureRecognizer进行设置,跟上面的代码稍微有点重复,如果你有代码洁癖的话可以考虑将以上代码进行惰性初始化,可能感官会更好一点,不过为了方便暂时都写在了一起: 1 2 3 4 5 6 7 8 self.centerX=CGRectGetWidth(self.view.bounds)/2; self.edgeView=[[UIView alloc]initWithFrame:CGRectMake(0, 320, CGRectGetWidth(self.view.bounds), 100)]; [self.edgeView setBackgroundColor:[UIColor greenColor]]; self.label=[[UILabel alloc]initWithFrame:CGRectMake(10, 30, 320, 40)]; [self.label setText: @"原文地址:http://www.cnblogs.com/xiaofeixiang/" ]; [self.label setFont:[UIFont systemFontOfSize:14]]; [self.edgeView addSubview:self.label]; [self.view addSubview:self.edgeView]; 注意这个时候手势是加载view不是单独的edgeView上的,手势代码,edges是一个枚举,我们可以设置的是响应边缘右滑事件; 1 2 3 4 5 UIScreenEdgePanGestureRecognizer *rightEdgeGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightEdgeGesture:)]; rightEdgeGesture.edges = UIRectEdgeRight; // 右滑显示 [self.view addGestureRecognizer:rightEdgeGesture]; 响应边缘事件的代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 //当前被触摸的view UIView *view = [self.view hitTest:[gesture locationInView:gesture.view] withEvent:nil]; if (UIGestureRecognizerStateBegan == gesture.state || UIGestureRecognizerStateChanged == gesture.state) { CGPoint translation = [gesture translationInView:gesture.view]; [UIView animateWithDuration:0.5 animations:^{ view.center = CGPointMake(self.centerX + translation.x, view.center.y); NSLog( @"%@" ,NSStringFromCGPoint(view.center)); }]; } else //取消,失败,结束的时候返回原处 { [UIView animateWithDuration:0.5 animations:^{ view.center = CGPointMake(self.centerX, view.center.y); }]; } 具体效果如下: 如果你细心点会发现那个篮球在滑动介结束的时候转动了一下,在处理动画结束的时候加了一个判断,代码如下: 1 2 3 4 5 6 7 8 9 10 if (gesture.state==UIGestureRecognizerStateEnded) { //旋转360度之后归0 if (self.currentRadius==360.f){ self.currentRadius=0.0f; } [UIView animateWithDuration:1.0 animations:^{ self.currentRadius += 90.0; self.circleView.transform = CGAffineTransformMakeRotation((self.currentRadius * M_PI) / 180.0); }]; } 如果你想那个篮球一直转动的话通过NSTimer即可实现: 1 [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(transformRotate) userInfo: nil repeats: YES]; 转动的代码和上面的差不多,不过每次改变的弧度较小: 1 2 3 4 5 6 7 8 -( void )transformRotate{ if (self.currentRadius==360.f){ self.currentRadius=0.0f; } else { self.currentRadius += 10.0; self.circleView.transform = CGAffineTransformMakeRotation((self.currentRadius * M_PI) / 180.0); } } 本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4725645.html,如需转载请自行联系原作者

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

iOS开发之UICollectionViewDataSourcePrefetching

在iOS10中,苹果为UICollectionViewCell引入了Pre-Fetching预加载机制用于提升它的性能。主要引入了一个新的数据源协议UICollectionViewDataSourcePrefetching,包含两个方法: @protocol UICollectionViewDataSourcePrefetching <NSObject> @required // 预加载数据 - (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0); @optional // 取消提前加载数据 - (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths NS_AVAILABLE_IOS(10_0); @end 网上搜了一大圈,讲述原理的(翻译文档)的文章很多,有干货的Demo很少,于是乎自己摸索了一下,写了一个简单的案例,在此记录并分享一下。 运行环境:Xcode 8.2.1 + iOS 10.2 核心步骤: 1、遵从 UICollectionViewDataSourcePrefetching 协议 2、实现 collectionView:prefetchItemsAtIndexPaths 方法和collectionView:cancelPrefetchItemsAtIndexPaths 方法(可选) 3、将第1步中遵从协议的类设置为 UICollectionView 的 prefetchDataSource 属性 实现 一、创建UICollectionViewFlowLayout 自己写一个类继承自UICollectionViewFlowLayout @implementation MyCollectionViewFlowLayout -(void)prepareLayout{ self.minimumLineSpacing = 1;//垂直间距 self.minimumInteritemSpacing = 0;//水平间距 self.sectionInset = UIEdgeInsetsMake(0, 0, 8, 0);//分组间距 } @end 二、用XIB定义一个 里面就一个UIImageView,然后拽线设置一个IBOutlet UICollectionViewCell.png @property (weak, nonatomic) IBOutlet UIImageView *imgView; 三、控制器 注释很详细 #import "ViewController.h" #import "MyCollectionViewFlowLayout.h" #import "ImgCollectionViewCell.h" #define ScreenW [UIScreen mainScreen].bounds.size.width //重用标识 static NSString *cellId = @"imgCell"; //遵守协议 @interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDataSourcePrefetching> //下载图片任务 @property(nonatomic, strong) NSMutableDictionary<NSURL *, dispatch_queue_t> *tasks; //图片地址 @property(nonatomic, copy) NSMutableArray<NSURL *> *imgURLArray; //下载的图片 @property(nonatomic, copy) NSMutableDictionary<NSURL *, UIImage *> *imgs; //UICollectionView @property(nonatomic, weak) UICollectionView *collectionView; @end @implementation ViewController //懒加载imgURLArray -(NSMutableArray<NSURL *> *)imgURLArray{ if (_imgURLArray == nil) { _imgURLArray = [NSMutableArray array]; for (int i = 0; i < 30; i++) { NSURL *imgURL = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1494499175005&di=1d8d40ac84f4a71cb26d7bf4a5a845ec&imgtype=0&src=http%3A%2F%2Fimg10.360buyimg.com%2Fyixun_zdm%2Fjfs%2Ft2830%2F11%2F2310606472%2F165925%2F962fa94a%2F575f7664Nfd743845.jpg"]; [_imgURLArray addObject:imgURL]; } } return _imgURLArray; } //懒加载imgs -(NSMutableDictionary<NSURL *,UIImage *> *)imgs{ if (_imgs == nil) { _imgs = [NSMutableDictionary dictionary]; } return _imgs; } //懒加载tasks -(NSMutableDictionary<NSURL *,dispatch_queue_t> *)tasks{ if (_tasks == nil) { _tasks = [NSMutableDictionary dictionary]; } return _tasks; } - (void)viewDidLoad { [super viewDidLoad]; //创建UICollectionView //创建布局 UICollectionViewLayout *layout = [[MyCollectionViewFlowLayout alloc]init]; //初始化一个UICollectionView UICollectionView *collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout]; //设置背景色 collection.backgroundColor = [UIColor groupTableViewBackgroundColor]; //设置代理 collection.dataSource = self; collection.delegate = self; collection.prefetchDataSource = self; //注册Cell UINib *nib = [UINib nibWithNibName:@"ImgCollectionViewCell" bundle:nil]; [collection registerNib:nib forCellWithReuseIdentifier:cellId]; [self.view addSubview:collection]; self.collectionView = collection; } -(void)loadImage:(NSIndexPath *)indexPath{ NSURL *currentURL = [self.imgURLArray objectAtIndex:indexPath.row]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); __weak typeof(self) weakSelf = self; //异步下载图片 dispatch_async(queue, ^{ NSData *imageData = [NSData dataWithContentsOfURL:currentURL]; UIImage *image = [UIImage imageWithData:imageData]; weakSelf.imgs[currentURL] = image; //更新UI dispatch_async(dispatch_get_main_queue(), ^{ ImgCollectionViewCell *cell = (ImgCollectionViewCell *)[weakSelf.collectionView cellForItemAtIndexPath:indexPath]; cell.imgView.image = image; }); }); //为了取消任务 self.tasks[currentURL] = queue; } #pragma mark <UICollectionViewDataSource> - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.imgURLArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ ImgCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath]; // 获取URL NSURL *imgURL = self.imgURLArray[indexPath.row]; //对应的URL的图片已经存在 if (self.imgs[imgURL]) { cell.imgView.image = self.imgs[imgURL]; } //不存在 else{ [self loadImage:indexPath]; } return cell; } #pragma mark <UICollectionViewDelegate> //定义每个Item 的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat W = (ScreenW - 1) / 2; return CGSizeMake(W, 100); } #pragma mark <UICollectionViewDataSourcePrefetching> - (void)collectionView:(UICollectionView *)collectionView prefetchItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths { for (NSIndexPath * indexPath in indexPaths) { NSURL *currentURL = [self.imgURLArray objectAtIndex:indexPath.row]; //不存在就请求 if (!self.imgs[currentURL]) { [self loadImage:indexPath]; } } } - (void)collectionView:(UICollectionView *)collectionView cancelPrefetchingForItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths { for (NSIndexPath * indexPath in indexPaths){ NSURL *currentURL = [self.imgURLArray objectAtIndex:indexPath.row]; //当前任务存在 if (self.tasks[currentURL]) { dispatch_queue_t queue = self.tasks[currentURL]; dispatch_suspend(queue); self.tasks[currentURL] = nil; } } } @end 效果 效果演示.gif 写在后面的话 1、这个新特性仍然需要探究 2、遇到的一个坑:细心看的话可以发现我的字典是懒加载的,如果直接在viewDidLoad中初始化会在 weakSelf.imgs[currentURL] = image; 一行报错,why?烦请知道的告知。 源代码

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

iOS开发Framework工程

新建Framwork工程 早期xcode可以创建.a的静态库或.dylib的动态库,现在(2017.1)一般直接创建Framework工程。 打开xcode,file-new-project,选择Cocoa Touch Framework就可以创建一个framework工程了。 新建工程 创建完成后默认的framework是动态库,点击Build Settings - Mach-O Type可以选择动态库还是静态库。 使用静态库 关于动态库和静态库的比较可以参考网上的资料,对于使用者而言,导入动态库唯一要注意的是在General - Embedded Binaries中需要添加对应动态库。 Bitcode支持 framework给用户使用需要支持bitcode,除了在Build Settings中需要把Enable Bitcode设为YES,还

资源下载

更多资源
Mario

Mario

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

腾讯云软件源

腾讯云软件源

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

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等操作系统。

用户登录
用户注册