码迷,mamicode.com
首页 > 其他好文 > 详细

九宫格图片浏览器

时间:2016-03-02 09:31:21      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

#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;//
@end

@implementation 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.
}
@end

九宫格图片浏览器

标签:

原文地址:http://www.cnblogs.com/zhangyunjiang-love/p/5233565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!