首页 文章 精选 留言 我的

精选列表

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

在iOS虚拟键盘上添加动态隐藏按钮

最近两周和团队做一个关于地理围栏技术的公交实时查询项目,为了给用户比较良好的交付,想在键盘上添加一个按钮,实时根据键盘不同高度变换按钮位置,再不做输入的时候点击按钮能够隐藏键盘,这种方式在很多软件上都有体现,然后在网上查阅了关于检测键盘高度一些相关知识,以下是一个Demo,代码有很多需要优化地方,仅供需要者参考; 先看效果: 首先是我们在ViewDidLoada()中注册了两个通知,[NSNotificationCenterdefaultCenter],检测键盘动态,一个是键盘将要弹出的时候,另一个是键盘将要退出时候键盘的信息 - (void)viewDidLoad { NSLog(@"%@",NSStringFromSelector(_cmd)); [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } 检测键盘消息一个六种,根据字面意思差不多都能说明函数作用 UIKeyboardWillShowNotification 通知将要发布时候显示键盘 UIKeyboardDidShowNotification 通知发布后立即显示键盘 UIKeyboardWillHideNotification 通知发布前撤销键盘 UIKeyboardDidHideNotification 通知发布后撤销键盘 UIKeyboardWillChangeFrameNotification 通知发布前迅速变化的框架的键盘。 UIKeyboardDidChangeFrameNotification 通知发布后立即改变在键盘的框架。 NSLog(@"%@",NSStringFromSelector(_cmd));是我特意加上去的,它能在控制台显示打印出当前程序所调用的函数,我在下面每个函数都加了这一句,当我进行不同操作的时候,打印出被调用函数名,在调试程序时候比较适用吧; 注册消息通知后,实现通知所响应的方法 - (void)handleKeyboardDidShow:(NSNotification *)notification { NSLog(@"%@",NSStringFromSelector(_cmd)); NSDictionary *info = [notification userInfo]; CGRect keyboardFrame; [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size; CGFloat distanceToMove = kbSize.height; NSLog(@"---->动态键盘高度:%f",distanceToMove); if (exitButton == nil) { exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; CGRect exitBtFrame = CGRectMake(self.view.frame.size.width-40, self.view.frame.size.height - distanceToMove, 40.0f, 30.0f); exitButton.frame = exitBtFrame; [exitButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateNormal]; [self.view addSubview:exitButton]; } exitButton.hidden=NO; [self adjustPanelsWithKeyBordHeight:distanceToMove]; [exitButton addTarget:self action:@selector(CancelBackKeyboard:) forControlEvents:UIControlEventTouchDown]; } 在这个函数方法中值得探讨的是关于键盘所包含信息,因为每一次键盘弹出的时候也是动画形式弹出,他的坐标位置大小包含在userInfo的字典中,现在我用 NSLog(@"-->info:%@",info);打印出info对象,这些信息都可以在不同存储类型,取值的时候注意取值方式,此处只是提一提,希望以后有时间在做探讨, 在这一段代码上,后面注释了5行,因为打算当键盘推出的时候,按钮从视图上移除,或者释放按钮,但是都导致了应用程序崩溃,后来就没有释放和移除操作了 - (void)handleKeyboardWillHide:(NSNotification *)notification { NSLog(@"%@",NSStringFromSelector(_cmd)); if (exitButton.hidden==NO) { exitButton.hidden = YES; } // if (exitButton.superview) // { // [exitButton removeFromSuperview]; // [exitButton release]; // } } -(void)adjustPanelsWithKeyBordHeight:(float) height { NSLog(@"%@",NSStringFromSelector(_cmd)); if (exitButton) { CGRect exitBtFrame = CGRectMake(self.view.frame.size.width - 40, self.view.frame.size.height - height-30, 40.0f, 30.0f); exitButton.frame = exitBtFrame; [self.view addSubview:exitButton]; } // UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; // if (exitButton.superview == nil) // { // [tempWindow addSubview:exitButton]; // // 注意这里直接加到window上 // } } -(void)CancelBackKeyboard:(id)sender { NSLog(@"%@",NSStringFromSelector(_cmd)); [textField resignFirstResponder]; } - (void)viewDidUnload { [self setTextField:nil]; exitButton=nil; [super viewDidUnload]; // Release any retained subviews of the main view. } - (void)dealloc { [textField release]; [exitButton release]; [[NSNotificationCenter defaultCenter] removeObserver:self];//移除所注册的通知 [super dealloc]; } 源代码:http://download.csdn.net/detail/duxinfeng2010/4831311 本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208706,如需转载请自行联系原作者

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

ios18---自定义控件3改进

控制器: // 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 /** * 懒加载,get方法 */ - (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 _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.创建一个商品*****************************/ /* XMGShopView *shopView = [[XMGShopView alloc] init]; shopView.frame = CGRectMake(x, y, width, height); // 设置数据 XMGShop *shop = self.dataArr[index]; shopView.shop = shop; [self.shopCarView addSubview:shopView]; */ XMGShopView *shopView = [XMGShopView shopViewWithShop:self.dataArr[index]]; shopView.frame = CGRectMake(x, y, width, height); [self.shopCarView addSubview:shopView]; /***********************3.设置数据*****************************/ // 设置数据 // XMGShop *shop = self.dataArr[index]; // [shopView setIcon:shop.icon]; // [shopView setName: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> @class XMGShop; @interface XMGShopView : UIView /** 商品模型 */ @property (nonatomic, strong) XMGShop *shop; // 构造方法 - (instancetype)initWithShop: (XMGShop *)shop; + (instancetype)shopViewWithShop: (XMGShop *)shop; @end // XMGShopView.m #import "XMGShopView.h" #import "XMGShop.h" @interface XMGShopView () /** 图片控件 */ @property (nonatomic, weak) UIImageView *iconView; /** 标题控件 */ @property (nonatomic, weak) UILabel *titleLabel; @end @implementation XMGShopView /** * 初始化子控件(不要设置frame) * */ - (instancetype)init{ if (self = [super init]) { [self setUp]; } return self; } //构造方法,对象方法 - (instancetype)initWithShop:(XMGShop *)shop{ if (self = [super init]) { // 注意:先创建后赋值,不然空指针, [self setUp]; self.shop = shop;//调用set方法 } return self; } //构造方法,类方法 + (instancetype)shopViewWithShop:(XMGShop *)shop{ return [[self alloc] initWithShop:shop]; } /** * 初始化 */ - (void)setUp{ // 1.创建商品的UIImageView对象 UIImageView *iconView = [[UIImageView alloc] init]; iconView.backgroundColor = [UIColor blueColor]; [self addSubview:iconView]; _iconView = iconView; // 2.创建商品标题对象 UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.backgroundColor = [UIColor yellowColor]; titleLabel.textAlignment = NSTextAlignmentCenter; // 居中 [self addSubview:titleLabel]; _titleLabel = titleLabel; } /** * 布局子控件(可以拿到frame) */ - (void)layoutSubviews{ // 0.一定要调用super [super layoutSubviews]; // 1.获取当前控件的尺寸 CGFloat width = self.frame.size.width; 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); } /** * 重写set方法:只要外边传数据就会调用 * 作用:设置数据 */ - (void)setShop:(XMGShop *)shop{ _shop = shop; // 设置数据 self.iconView.image = [UIImage imageNamed:shop.icon]; self.titleLabel.text = shop.name; } @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/7466861.html,如需转载请自行联系原作者

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

iOS开发-CGAffineTransformMakeRotation改变了中心解决的方法

坑爹的。 为了这个问题折腾了2个小时。 恼.. 今天在写一个时钟demo的时候, 时针的旋转用到了CGAffineTransformMakeRotation, 按理说. 图像的旋转是以图像本身的中心(center)为锚点的, 也就是在旋转过程中, 它的中心是固定的。 就和我们时针的效果一样。 比方我以下的时针, 分针, 秒针旋转的代码: NSDate *today = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; unsigned flags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *todayComponents = [calendar components:flags fromDate:today]; int hour = [todayComponents hour]; int min = [todayComponents minute]; int sec = [todayComponents second]; float fineHour = (hour % 12) + min /60.0; hourHand.transform = CGAffineTransformMakeRotation(M_PI * 2 * fineHour / 12.0); minuteHand.transform = CGAffineTransformMakeRotation(M_PI * 2 * min / 60.0); secondHand.transform = CGAffineTransformMakeRotation(M_PI * 2 * sec / 60.0); 代码是没问题的。 可是, 执行起来的时候, 发现旋转过程中中心变了。 出现了明显的位移... 折腾了半天。 原来是自己主动布局搞的鬼.. 蛋疼。 解决的办法: 1. 关掉自己主动布局。 2.你应当设置center的约束,而不是边缘位置的约束。 攻克了。 睡觉了.. 本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5239612.html,如需转载请自行联系原作者

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

iOS:实现邮件和短信发送的简单示例

发送邮件1.导入库文件:MessageUI.framework2.引入头文件3.实现代理<MFMailComposeViewControllerDelegate> 和 <UINavigationControllerDelegate>代码示例: - (void)didClickSendEmailButtonAction{ if ([MFMailComposeViewController canSendMail] == YES) { MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init]; // 设置代理(与以往代理不同,不是"delegate",千万不能忘记呀,代理有3步) mailVC.mailComposeDelegate = self; // 收件人 NSArray *sendToPerson = @[@"humingtao2014@gmail.com"]; [mailVC setToRecipients:sendToPerson]; // 抄送 NSArray *copyToPerson = @[@"humingtao2013@126.com"]; [mailVC setCcRecipients:copyToPerson]; // 密送 NSArray *secretToPerson = @[@"563821250@qq.com"]; [mailVC setBccRecipients:secretToPerson]; // 主题 [mailVC setSubject:@"hello world"]; [self presentViewController:mailVC animated:YES completion:nil]; [mailVC setMessageBody:@"魑魅魍魉,哈哈呵呵嘿嘿霍霍" isHTML:NO]; }else{ NSLog(@"此设备不支持邮件发送"); } } - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{ switch (result) { case MFMailComposeResultCancelled: NSLog(@"取消发送"); break; case MFMailComposeResultFailed: NSLog(@"发送失败"); break; case MFMailComposeResultSaved: NSLog(@"保存草稿文件"); break; case MFMailComposeResultSent: NSLog(@"发送成功"); break; default: break; } [self dismissViewControllerAnimated:YES completion:nil]; } // 系统发送,模拟器不支持,要用真机测试 - (void)didClickSendSystemEmailButtonAction{ NSURL *url = [NSURL URLWithString:@"humingtao2014@gmail.com"]; if ([[UIApplication sharedApplication] canOpenURL:url] == YES) { [[UIApplication sharedApplication] openURL:url]; }else{ NSLog(@"此设备不支持"); } } 发送短信前面三步引入配置和邮件发送一样 // 调用系统API发送短信 - (void)didClickSendMessageButtonAction{ if ([MFMessageComposeViewController canSendText] == YES) { MFMessageComposeViewController *messageVC = [[MFMessageComposeViewController alloc] init]; // 设置代理<MFMessageComposeViewControllerDelegate> messageVC.messageComposeDelegate = self; // 发送To Who messageVC.recipients = @[@"18757289870"]; messageVC.body = @"hello world"; [self presentViewController:messageVC animated:YES completion:nil]; }else{ NSLog(@"此设备不支持"); } } - (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{ switch (result) { case MessageComposeResultCancelled: NSLog(@"取消发送"); break; case MessageComposeResultFailed: NSLog(@"发送失败"); break; case MessageComposeResultSent: NSLog(@"发送成功"); break; default: break; } [self dismissViewControllerAnimated:YES completion:nil]; } // 调用系统应用程序发送消息 - (void)didClickSendMessage2ButtonAction{ NSURL *url = [NSURL URLWithString:@"sms:18656348970"]; if ([[UIApplication sharedApplication] canOpenURL:url] == YES) { [[UIApplication sharedApplication] openURL:url]; }else{ NSLog(@"失败"); } } 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5127992.html ,如需转载请自行联系原作者

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

iOS:扩展UIColor,支持十六进制颜色设置

来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #defineUIColorCategoryUIBarButtonItem 新建一个Category,命名为UIColor+Hex,表示UIColor支持十六进制Hex颜色设置。 UIColor+Hex.h文件 #import <UIKit/UIKit.h> //RGB颜色设置 #define RGBA_COLOR(R, G, B, A) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:A] #define RGB_COLOR(R, G, B) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:1.0f] @interface UIColor (Hex) + (UIColor *)colorWithHexString:(NSString *)color; //从十六进制字符串获取颜色, //color:支持@“#123456”、 @“0X123456”、 @“123456”三种格式 + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha; @end 上面的代码在开头是两个宏定义,就是对[UIColor colorWithRed:green:blue:alpha]方法的简化,在UIColor(Hex)中声明两个方法-colorWithHexString和-colorWithHexString:alpha,这个很好理解。 UIColor+Hex.m文件 #import "UIColor+Hex.h" @implementation UIColor (Hex) + (UIColor *)colorWithHexString:(NSString *)color alpha:(CGFloat)alpha { //删除字符串中的空格 NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be 6 or 8 characters if ([cString length] < 6) { return [UIColor clearColor]; } // strip 0X if it appears //如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾 if ([cString hasPrefix:@"0X"]) { cString = [cString substringFromIndex:2]; } //如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾 if ([cString hasPrefix:@"#"]) { cString = [cString substringFromIndex:1]; } if ([cString length] != 6) { return [UIColor clearColor]; } // Separate into r, g, b substrings NSRange range; range.location = 0; range.length = 2; //r NSString *rString = [cString substringWithRange:range]; //g range.location = 2; NSString *gString = [cString substringWithRange:range]; //b range.location = 4; NSString *bString = [cString substringWithRange:range]; // Scan values unsigned int r, g, b; [[NSScanner scannerWithString:rString] scanHexInt:&r]; [[NSScanner scannerWithString:gString] scanHexInt:&g]; [[NSScanner scannerWithString:bString] scanHexInt:&b]; return [UIColor colorWithRed:((float)r / 255.0f) green:((float)g / 255.0f) blue:((float)b / 255.0f) alpha:alpha]; } //默认alpha值为1 + (UIColor *)colorWithHexString:(NSString *)color { return [self colorWithHexString:color alpha:1.0f]; } @end 这样就扩展了UIColor,支持十六进制颜色设置。下面举个栗子,设置UIButton一些颜色特征,来说明该扩展的使用, #import "UIColor+Hex.h" //省略多余的代码 //设置导航栏右侧的BarButtonItem为Button - (void)setupNavigationItem { UIView *rightView = [[UIView alloc] init]; rightView.bounds = CGRectMake(0, 0, 52, 44); UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame = CGRectMake(-6, 0, 52, 44); rightButton.backgroundImageEdgeInsets = UIEdgeInsetsMake(7, 0, 7, 0); //kSetting是国际化的字符串"设置" [rightButton setTitle:NVSLocalizedString(@"kSetting", nil) forState:UIControlStateNormal]; //使用宏定义的RGB_COLOR // [rightButton setTitleColor:RGB_COLOR(160, 170, 150) forState:UIControlStateHighlighted]; //使用UIColor+Hex扩展 [rightButton setTitleColor:[UIColor colorWithHexString:@"#708c3b"] forState:UIControlStateNormal]; rightButton.titleLabel.font = [UIFont fontWithName:@"Heiti SC" size:12.f]; [rightButton setBackgroundImage:[UIImage imageNamed:@"device_setting_bg"] forState:UIControlStateNormal]; [rightButton setBackgroundImage:[UIImage imageNamed:@"device_setting_bg_press"] forState:UIControlStateHighlighted]; [rightButton addTarget:self action:@selector(settingBtnPresss:) forControlEvents:UIControlEventTouchUpInside]; [rightView addSubview:rightButton]; UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightView]; [self.navigationItem setRightBarButtonItem:rightBarButtonItem animated:YES]; [rightBarButtonItem release]; [rightView release]; } 使用差不多就这么简单,总结一下,本篇博客主要有以下几个细节或者说知识点, (1)宏定义RGB_COLOR和RGBA_COLOR可以设置颜色 (2)UIColor+Hex扩展可以设置颜色 (3)导航栏上面的BarButtonItem怎么设置为Button (4)Button一些常用和不常用的属性设置 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5127233.html ,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

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

Mario

Mario

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

腾讯云软件源

腾讯云软件源

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

Spring

Spring

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

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