IOS之每天总结一点点更新中。。。

本文汇总了iOS开发中常用的技巧,包括界面元素定制、动画效果实现、日期格式处理等,特别适用于希望提高应用用户体验的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.chinadev.cn/  天气预报接口
 /***************************************************************************/
拉伸 图片

UIImage *normalImage = [[UIImage imageNamed:@"whiteButton.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
/***************************************************************************/
【隐藏 键盘】
触摸背景隐藏,最里层放个视图大小的button,设置Custom风格透明,连线到
-(IBAction)hiddenKeyboard:(id)sender
     [sender resignFirstResponder];
}
/***************************************************************************/
【字符串 替换
myString Hello town -----> Hello worldmyNewString 
myNewString = [myString stringByReplacingOccurrencesOfString:@"town" withString:@"world"];
/***************************************************************************/
【分段控件 标题获取
[mySegment titleForSegmentAtIndex:mySegment.selectedSegmentIndex]; // 通过索引号获取相对应的标题
/***************************************************************************/
【LCD 电子时钟字体
 
DBLCDTempBlack   
 图片
/***************************************************************************/
【随机数 范围
arc4random()%12     随机数 范围是 0~11         
[self.array count]==12;   数组下标 范围是0~11     
结论 : arc4random()%[self.array count] == 数组下标
/***************************************************************************/
【加载 资源图片
#define FH_GetWithImage(a,b) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:a ofType:b]]
[img setImage:FH_GetWithImage(@"imgName"@"png")];   // 加载图片尽量这样写 
[UIImage imageNamed:@"imgName. png"];   // 不要这样写,图片多了就不好了
/***************************************************************************/
【列表 背景
 44*44 大小的图片 设置背景颜色 平铺 就可以做背景图
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"1.png"]]];
设置 列表透明  [self.tableView setBackgroundColor:[UIColor clearColor]];
设置 箭头 标记  44*44 大小的图片 可以不设置frame的宽和高。
UIImageView *arrowImg = [[UIImageView allocinitWithImage:[UIImage imageNamed:@"2.png"]];
CGRect rect = arrowImg.frame;
rect.origin.x = 230;
arrowImg.frame = rect;
[cell.contentView addSubview: arrowImg];
/***************************************************************************/

    self.tableView.tableHeaderView=self.searchBar;    // 搜索栏和列表一起滚动

    self.tableView.rowHeight = 60;

/***************************************************************************/
【cell 分割线图片
[TableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];  // 把默认的分割线设置为无
UIImageView *line = [[UIImageView allocinitWithFrame:CGRectMake
(0, cell.bounds.size.height - 1, cell.bounds.size.width1)];   // 分割线图片高1个像素,图片长就是cell长
[line setImage:FH_GetWithImage(@"line"@"png")];
[cell.contentView addSubview:line];
/***************************************************************************/
【cell 背景颜色】 
 
cell.selectedBackgroundView = [[UIView allocinitWithFrame:cell.bounds];
 
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:47.0/255.0 green:47.0/255.0 blue:47.0/255.0 alpha:1.0]; 
/***************************************************************************/
【pageController 颜色     
[mypagecontrol setCurrenPageIndicatorTintColor:[UIColor redColor]];
[mypagecontrol setPageIndicatorTintColor:[UIColor blackColor]];
/***************************************************************************/
 
【图像 动画
UIImageView 对象  属性: 默认是 每 秒 30 帧 播放速度
animationImages       存放图片的数组   (NSArray)
isAnimating                正在以动画方式播放图片 (BOOL)
startAnimating           开始播放动画
stopAnimating            停止播放动画
/***************************************************************************/
输入信息有误,左右晃动文本框
 
- (void)animateIncorrectMessage:(UIView *)view    //这个view代表账号和密码的整体

CGAffineTransform moveRight = CGAffineTransformTranslate(CGAffineTransformIdentity80);

CGAffineTransform moveLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -80);

CGAffineTransform resetTransform = CGAffineTransformTranslate(CGAffineTransformIdentity00);

    [UIView animateWithDuration:0.1 animations:^{

        view.transform = moveLeft;

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.1 animations:^{

            view.transform = moveRight;

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.1 animations:^{

                view.transform = moveLeft;

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:0.1 animations:^{

                    view.transform = moveRight;

                } completion:^(BOOL finished) {

                    [UIView animateWithDuration:0.1 animations:^{

                        view.transform = resetTransform;

                    }];

                }];

            }];

        }];

    }];
}
/***************************************************************************/
 
【日期 格式
 
创建日期格式对象 NSDateFormatter *dateFormat = [[NSDateFormatter  alloc] init];

设置 显示格式  [dateFormat  setDateFormat:@"MMMM d ,yyyy  hh:mm:ssa"]; 
// MMMM月,d日,yyyy年      hh时,mm分,ss秒,a上下午
字符串 显示当前时间格式  todaysDateString = [
dateFormat  srtingFromDate:[NSDate date]];  
/***************************************************************************/
 
【两日期相差 多少天
-(void)calculateDateDifference:(NSDate*)choseDate

NSDate *todaysDate = 
[NSDate date];
NSTimeInterval *difference = [
todaysDate  timeIntervalSinceDate:chosenDate]/86400;
// 除以一天的秒数得到天数         1天 = 86400秒
NSDateFormatter *dateFormat = [[NSDateFormatter  alloc] init]; 
[dateFormat  setDateFormat:@"MMMM d ,yyyy  hh:mm:ssa"]; 
todaysDateString = [dateFormat string FromDate:todaysDate]; 
chosenDateString = [dateFormat string FromDate:chosenDate];
输出:NSString *differenceOutput = [[NSString alloc] initWithFormat:@"Between chosen date (%@) and 
today(%@) in days: %1.2f",chosenDateString,todaysDateString,fabs(difference)];  // C方法取绝对值

-(IBAction)setDateTime:(id)sender
{    
           // 
(DateChooserViewController ) self.delegate = self (ViewController ) 
       [(ViewController *)self.delegate  calculateDateDifference:(UIDatePicker *)sender.date ]; 
         // 选择了日期  
}
②-(void)viewDidAppear:(BOOL)animated
{
         // (DateChooserViewController ) self.delegate = self (ViewController )
     
 
[(ViewController *)self.delegate  calculateDateDifference:[NSDate date]]; 
       
// 取消选择了日期,未选择日期。返回当前日期

/***************************************************************************/
【绘制按钮 普通和高亮
】---小图片(15*15)贴大按钮(45*45),不会自动拉伸变形
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

closeButton.frame = CGRectMake(320 - 45.0f,0.0f,45.0f,45.0f);

closeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

    CGRect rect = CGRectMake(0.0f0.0f45.0f45.0f);

    UIImage *closeButtonImage = [UIImage imageNamed:@"close.png"];
/***************************************************************************/
     
// 绘制高亮  原理:获取按钮Size大小的画布并填充FillRect黑色深一点的颜色,
          将原图closeButtonImage绘制drawInRect在画布上,保留alpha再截取画布图片(45*45)做高亮图

    UIGraphicsBeginImageContextWithOptions(rect.sizeNO0.0f); // 绘制大小,透明,比例

    CGContextRef context = UIGraphicsGetCurrentContext();    

    CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:35.0/255.0 green:35.0/255.0 blue:35.0/255.0 alpha:1.0CGColor]);

    CGContextFillRect(context, rect);

    [closeButtonImage drawInRect:CGRectMake(15.0f,15.0f,closeButtonImage.size.width,closeButtonImage.size.heightblendMode:kCGBlendModeNormal alpha:1.0]; 
 
// 图片是15*15 按钮是 45*45  x,y坐标=(45-15)/2=15.0f

    UIImage *highlightedButtonImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext(); // 绘制结束

    [closeButton setBackgroundImage:highlightedButtonImage forState:UIControlStateHighlighted];
/***************************************************************************/
     
// 绘制普通  原理:获取按钮Size大小的画布,
          将原图
closeButtonImage绘制drawInRect在画布上,alpha改0.5半透明,再截取画布图片(45*45)做普通图

    UIGraphicsBeginImageContextWithOptions(rect.sizeNO0.0f); // 绘制大小,透明,比例

    UIGraphicsGetCurrentContext();

    [closeButtonImage drawInRect:CGRectMake(15.0f,15.0f,closeButtonImage.size.width,closeButtonImage.size.height) blendMode:kCGBlendModeNormal alpha:0.5];
 // 图片是15*15 按钮是 45*45  x,y坐标=(45-15)/2=15.0f 

    UIImage *normalButtonImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext(); // 绘制结束

    [closeButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
/***************************************************************************/
 
改变 pagecontrol样式

- (void)viewDidLoad

{
    [super viewDidLoad];

    [self setCurrentPage:self.page.currentPage]; // 一开始调用这个方法就改变pagecontrol样式
 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
 
    self.page.currentPage=scrollView.contentOffset.x/320;

    [self setCurrentPage:self.page.currentPage]; // 滚动切换 再调用这个方法改变pagecontrol样式
}
- (void) setCurrentPage:(NSInteger)currentPage
{
 
     for (NSInteger indexPage = 0; indexPage < [page.subviews count]; indexPage++)
 
      {
 
           UIImageView * subview = [page.subviews objectAtIndex:indexPage];
            
CGSize size;
            
size.height = 24/2;
            
size.width = 24/2;
            
[subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y,

                                     size.width,size.height)];
         
 if (indexPage == currentPage)
         
 {  
                 
[subview setImage:[UIImage imageNamed:@"a.png"]];
          
}
     
     else
             
{ 
                 
[subview setImage:[UIImage imageNamed:@"d.png"]];
             
}
        
}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值