iOS UIScrollView+UIImageView 制作简单的图片浏览器,支持缩放、平移

做一个简单的图片浏览器,支持缩放、平移。本想自己用手势处理图片的缩放和平移,但经过搜索引擎一搜索,发现可以借助UIScrollView的缩放功能,完美实现图片的缩放和平移。当前,中途也遇到缩放后图片没有居中显示,或者即使居中显示了,但是平移时发现图片一边到不了边,另一边却留很多空,找到了答案。下面我整理了下代码,发布出来.

首先是主界面代码;

#import "MainViewController.h"

#import "TowViewController.h"

#import "OneViewController.h"   

#import "MyButton.h"

@interface MainViewController ()


@end


@implementation MainViewController

-(void)dealloc

{

    

    [super dealloc];

}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        

    }return self;

}





- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    [self cover];


}

-(void)cover

{

    MyButton *aButton=[[MyButton alloc]initWithFrame:CGRectMake(20, 20, self.view.frame.size.width/3, self.view.frame.size.height/4)];

    UIImage *temp=[UIImage imageNamed:@"1.jpg"];

    [aButton setImage:temp forState:UIControlStateNormal];

    [aButton addTarget:self action:@selector(goToshanghai:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:aButton];

    [temp release];

    [aButton release];

    

    MyButton *bButton=[[MyButton alloc]initWithFrame:CGRectMake(20+aButton.frame.size.width,20,250, self.view.frame.size.height/4)];

//    [bButton setBackgroundColor:[UIColor redColor]];

    [bButton setTitle:@"小宝贝我来上海啦" forState:UIControlStateNormal];

    [bButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    bButton.titleLabel.font=[UIFont systemFontOfSize:20];

    [bButton addTarget:self action:@selector(goToshanghai:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bButton];

    [bButton release];

    

    MyButton *aButton1=[[MyButton alloc]initWithFrame:CGRectMake(20, aButton.frame.size.height+40, self.view.frame.size.width/3, self.view.frame.size.height/4)];

    UIImage *temp1=[UIImage imageNamed:@"2.jpg"];

    [aButton1 setImage:temp1 forState:UIControlStateNormal];

    [aButton1 addTarget:self action:@selector(goToMydalian:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:aButton1];

    [temp1 release];

    [aButton1 release];

    MyButton *bButton1=[[MyButton alloc]initWithFrame:CGRectMake(20+aButton.frame.size.width,aButton.frame.size.height+40,250, self.view.frame.size.height/4)];

//    [bButton1 setBackgroundColor:[UIColor redColor]];

    [bButton1 setTitle:@"小宝贝来了大连啦" forState:UIControlStateNormal ];

    [bButton1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

     bButton1.titleLabel.font=[UIFont systemFontOfSize:20];

    [bButton1 addTarget:self action:@selector(goToMydalian:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:bButton1];

    [bButton1

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值