你的位置:首页 > 操作系统

[操作系统]九宫格图片浏览器

#import "ViewController.h"
#import "Appfz.h"
@interface ViewController ()
@property(nonatomic,copy)NSArray *apps;//文件读取
@property(nonatomic,assign)BOOL b;//判断属于放大还是缩小方法
@property(nonatomic,assign)CGRect cgr;//记录按钮按下时的按钮的坐标、尺寸
-(NSArray *)apps;//
@[email protected] ViewController
//数据读取
-(NSArray *)apps
{
    if (_apps==nil) {
    NSString *path=[[NSBundle mainBundle]pathForResource:@"myhous.plist" ofType:nil];
    NSArray *arr=[NSArray arrayWithContentsOfFile:path];
    NSMutableArray *arrM=[NSMutableArray array];
    for (NSDictionary *dic in arr) {
        Appfz *app=[Appfz appWithdic:dic];
        [arrM addObject:app];
        }
    _apps=arrM;
    }
    return _apps;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //基本数据
    int index=4;
    CGFloat appW=95;
    CGFloat appH=95;
    CGFloat apptopY=30;
    CGFloat appjxX=(self.view.frame.size.width-index*appW)/(index+1);
    CGFloat appjxY=appjxX;
    for (int i=0; i<self.apps.count; i++) {
        int X=i/index;
        int Y=i%index;
        Appfz *app1=self.apps[i];
        //循环添加按钮
        UIButton *app=[[UIButton alloc]init];
        app.frame=CGRectMake(appjxX+(appW+appjxX)*Y,apptopY+(appH+appjxY)*X+appjxY, appW, appH);
        [app setBackgroundImage:[UIImage imageNamed:app1.icon] forState:UIControlStateNormal];
        [app setBackgroundImage:[UIImage imageNamed:app1.icon] forState:UIControlStateHighlighted];
        [self.view addSubview:app];
        [app addTarget:self action:@selector(isbutton:) forControlEvents:UIControlEventTouchDown];
        }
    }
//定义按钮方法
-(IBAction)isbutton:(UIButton *)app
{
    if (self.b==NO) {
        //在放大之前将按钮的大小尺寸赋值给self.cgr
        self.cgr=app.frame;
        [self button:app];
    }
    else
    {
        [self smallImgClick:app];}
}
//放大图像
-(void)button:(UIButton *)app
{
    [UIView animateWithDuration:1.0 animations:^{
        app.frame=CGRectMake(0, 30, 414, 706);
    }];
    [self.view bringSubviewToFront:app];
    //放大后不再放大
    self.b=YES;
}
//图像还原
-(void)smallImgClick:(UIButton *)app1
{
        [UIView animateWithDuration:1.0 animations:^{
           app1.frame=self.cgr;
        }];
    [self.view bringSubviewToFront:app1];
    self.b=NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
[email protected]