月度归档:2016年05月

IOS—解析JSON(一)

JSON 作为一种轻量级的数据交换格式,应用于互联网各处,下文将展示利用IOS自带的JSON解析方法,进行JSON数据解析,代码 如下所示:

//  ViewController.h
//  Copyright © 2016年 www.maomao365.com. All rights reserved.
#import 
 
@interface ViewController : UIViewController
{
     UITextView * _uiTextViewTest;
    UITextField * _uiTextField;
}
 
@end

//  ViewController.m
//  Created by shenye on 16/4/21.
//  Copyright © 2016年 www.maomao365.com  All rights reserved.
//

#import "ViewController.h"
 
@interface ViewController ()

@end

@implementation ViewController

//按钮事件
-(void)btnClick:(id)sender{
 
    _uiTextViewTest.text =@"准备解析web地址";
    if([_uiTextField.text isEqual:@""]){
       _uiTextViewTest.text = @"当前web地址不能为空!";
       return;
    }
    
    if(![_uiTextField.text indexOfAccessibilityElement:@"http"]){
       _uiTextViewTest.text = @"不是合法的web地址";
        return;
    }

    [self showJsonInfoByWebAdd];//解析JSON
    
}

//解析JSON方法
-(void)showJsonInfoByWebAdd{
    NSError *error;
    //加载一个NSURL对象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_uiTextField.text]];
    //将请求的url数据放到NSData对象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    NSDictionary *info = [dic objectForKey:@"weatherinfo"];
    _uiTextViewTest.text = [NSString stringWithFormat:@"城市 %@ 气温: %@  风向: %@  最后检测时间:%@",[info objectForKey:@"city"],[info objectForKey:@"temp"],[info objectForKey:@"WD"], [info objectForKey:@"time"] ];   
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
     //JSON解析 示例 start
     //生成界面
    UITextView *uiTextViewTest = [[UITextView alloc]initWithFrame:CGRectMake(20,20, self.view.frame.size.width-30, 300)];
    uiTextViewTest.layer.borderColor = [[UIColor grayColor]CGColor];
    uiTextViewTest.layer.borderWidth = 2.0f;
    
    [self.view addSubview:uiTextViewTest];
    _uiTextViewTest = uiTextViewTest;
    
    UILabel *uiLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 350, 100, 30)];
    [uiLabel setText:@"web地址:"];
    [self.view addSubview:uiLabel];
    
    UITextField *uiTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 350, self.view.frame.size.width-110, 30)];
    uiTextField.layer.borderColor =[[UIColor grayColor] CGColor];
    uiTextField.layer.borderWidth = 2.0f;
    uiTextField.text =@"http://www.weather.com.cn/data/sk/101010100.html";
    [self.view addSubview:uiTextField];
    _uiTextField = uiTextField;
    
    
    
    
    
    UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(30,400, 150, 50)];
    [button1 setBackgroundColor:[UIColor grayColor]];
    [button1 setTitle:@"解析JSON" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:button1];
    
    
    
     //JSON解析 示例 end
    
    return;
}

运行效果如下图所示:

navigation_controller1

IOS控件-UIScrollView简介(二)

下文主要讲述如何生成一个UIScrollView,和通过按钮来控制UIScrollView的滚动效果


//  ViewController.h
//  Created by shenye on 16/4/21.
//  Copyright © 2016年 maomao365.com. All rights reserved.
// 
#import < UIKit / UIKit.h>
@interface ViewController : UIViewController
{ 
    UIScrollView * _uiScrollViewTest; //定义一个UIScrollView对象缓存当前对象
} 
@end

 
//  ViewController.m
//  Created by shenye on 16/4/21.
//  Copyright © 2016年 maomao365.com. All rights reserved.
 
#import "ViewController.h"
 
@interface ViewController ()

@end

@implementation ViewController
 //定义按钮事件,让按钮来控制UIScrollView的滚动效果
-(void)buttonTestClick:(UIButton *)sender{
    NSString *t =  [sender titleForState:UIControlStateNormal];
   CGPoint offset = _uiScrollViewTest.contentOffset; //获取当前UIScrollView对象的内容偏移量
    //根据不同的按钮,显示不同的滚动方式
   if([t  isEqual: @"垂直滚动"])
    {
    offset.y +=30;
    if(offset.y+_uiScrollViewTest.frame.size.height >_uiScrollViewTest.contentSize.height)
    {
        offset.y =_uiScrollViewTest.contentSize.height-_uiScrollViewTest.frame.size.height;
    }
    if(offset.y<0){
        offset.y =0;
    }   
    }
    
    
    if([t  isEqual: @"水平滚动"])
    {
        offset.x +=30;
        if(offset.x+_uiScrollViewTest.frame.size.width >_uiScrollViewTest.contentSize.width)
        {
            offset.x =_uiScrollViewTest.contentSize.width-_uiScrollViewTest.frame.size.width;
        }
        if(offset.x<0){
            offset.x =0;
        }
        
    }
    
    
    
    _uiScrollViewTest.contentOffset = offset;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //生成一个UIScrollView控件
    UIScrollView *uiScrollViewTest = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,600 , 600)];
    [uiScrollViewTest setBackgroundColor:[UIColor yellowColor]]; 
    [self.view addSubview:uiScrollViewTest]; //添加uiScrollView到当前视图中
    
    
    //创建一个图片view
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image =  [UIImage imageNamed:@"2.png"];
    CGFloat imageW = imageView.image.size.width;
    CGFloat imageH = imageView.image.size.height;
    imageView.frame = CGRectMake(0, 0, imageW, imageH);
    [uiScrollViewTest addSubview:imageView];
    
    //设置scrollView的滚动范围
    uiScrollViewTest.contentSize = imageView.image.size;
    
    //隐藏scrollView的垂直和水平的滚动条显示
    uiScrollViewTest.showsHorizontalScrollIndicator = NO;
    uiScrollViewTest.showsVerticalScrollIndicator   = NO;
    
    uiScrollViewTest.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);  //设置内容区域外的滚动边框
    

    _uiScrollViewTest = uiScrollViewTest;
    
    //view生成按钮控制滚动
    UIButton *buttonTest = [[UIButton alloc]initWithFrame:CGRectMake(330, 630, 100, 40)];
    
    [buttonTest setBackgroundColor:[UIColor grayColor]];
    [buttonTest addTarget:self action:@selector(buttonTestClick:)  forControlEvents:UIControlEventTouchDown];
    [buttonTest setTitle:@"垂直滚动" forState:UIControlStateNormal];
    [self.view addSubview:buttonTest];
    
    
    UIButton *buttonTest2 = [[UIButton alloc]initWithFrame:CGRectMake(230, 630, 100, 40)];
    
    [buttonTest2 setBackgroundColor:[UIColor grayColor]];
    [buttonTest2 addTarget:self action:@selector(buttonTestClick:)  forControlEvents:UIControlEventTouchDown];
    [buttonTest2 setTitle:@"水平滚动" forState:UIControlStateNormal];
    [self.view addSubview:buttonTest2];
  }

相关阅读:
IOS控件-UIScrollView简介(一)

IOS控件-UIScrollView简介(一)


一、UIScrollView控件简介


当IOS屏幕内容不足以展示所有内容时,此时我们需要使用UIScrollView控件来控制显示屏幕以外的内容。


二、UIScrollView使用

当将需要显示的内容添加到UIScrollView控件中,通过设置contentSize属性,我们就可以知道整个UIScrollView的内容尺寸,然后UIScrollView可以在内容范围内进行滚动

三、UIScrollView属性

@property(nonatomic)CGPointcontentOffset; //UIScrollView滚动位置
@property(nonatomic)CGSizecontentSize; //UIScrollView内容尺寸,控制滚动范围
@property(nonatomic)UIEdgeInsetscontentInset; //增加额外滚动区域(常用于)

@property(nonatomic) BOOL bounces; // 是否有缩放弹动效果

@property(nonatomic,getter=isScrollEnabled)BOOLscrollEnabled; // UIScrollView是否能滚动

@property(nonatomic) BOOL showsHorizontalScrollIndicator; //UIScrollView 是否显⽰⽔水平滚动条

@property(nonatomic) BOOL showsVerticalScrollIndicator; //是否UIScrollView 是否显⽰垂直滚动条


四、UIScrollView方法

– (void)scrollViewDidScroll:(UIScrollView *)scrollView; //任何UIScrollView的内容尺寸变化都会触发 // any offset changes
– (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // 任何缩放的变化

// called on start of dragging (may require some time and or distance to move)
– (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; //当UIScrollView开始滚动时触发
// called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest
– (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
– (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

– (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; // 当开始移动时执行方法
– (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; // 当滚动操作停止后

– (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; //

– (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; // 当进行UIScrollView缩放操作

– (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); // 开始缩放时执行
– (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; // 缩放结束前执行

– (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; // 当快要滚动到顶端的时候,返回yes,执行此方法
– (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView; // 当UIScrollView滚动到top后执行

相关阅读:
IOS控件-UIScrollView简介(二)