UI基础第一天(代码)

来源:互联网 发布:Vue.js 更改class 编辑:程序博客网 时间:2024/05/17 01:45

编程任务:

        设计可以移动和缩放图片的APP,如下图,掌握按钮的常见属性和常见方法。

                                    

方法一:利用frame属性,对图片的位置和大小进行更改。

#import "ViewController.h"@interface ViewController ()//私有属性@property (nonatomic,weak) IBOutlet UIButton* photoButton;//调整frame-(void)frameX:(CGFloat)x Y:(CGFloat)y height:(CGFloat)h width:(CGFloat)w;//向上移动-(IBAction)up;//向下移动-(IBAction)down;//向左移动-(IBAction)left;//向右移动-(IBAction)right;//缩小-(IBAction)small;//放大-(IBAction)big;@end@implementation ViewController//调整frame-(void)frameX:(CGFloat)x Y:(CGFloat)y height:(CGFloat)h width:(CGFloat)w{    //取出photoButton的frame    CGRect frame = self.photoButton.frame;    //调整frame    frame.origin.x = frame.origin.x + x;    frame.origin.y = frame.origin.y + y;    frame.size.height = frame.size.height + h;    frame.size.width = frame.size.width + w;    //更新photoButton的frame    self.photoButton.frame = frame;}//向上移动-(IBAction)up{    [self frameX:0 Y:-10 height:0 width:0];}//向下移动-(IBAction)down{    [self frameX:0 Y:+10 height:0 width:0];}//向左移动-(IBAction)left{    [self frameX:-10 Y:0 height:0 width:0];}//向右移动-(IBAction)right{    [self frameX:+10 Y:0 height:0 width:0];}//缩小-(IBAction)small{    [self frameX:0 Y:0 height:-10 width:-10];}//放大-(IBAction)big{    [self frameX:0 Y:0 height:+10 width:+10];}@end

方法二:利用tag,center,bounds属性,使用开关语句,对图片的位置和大小进行更改

#import "ViewController.h"@interface ViewController ()//私有属性@property (nonatomic,weak) IBOutlet UIButton* photoButton;//调整位置- (IBAction) move:(UIButton *) sender;//调整大小- (IBAction) size:(UIButton *) sender;@end@implementation ViewController//调整位置- (IBAction) move:(UIButton *) sender{    //取出center    CGPoint center = self.photoButton.center;    //调整center    switch (sender.tag) {        case 1:            center.y -= 10;            break;        case 2:            center.y += 10;            break;        case 3:            center.x -= 10;            break;        case 4:            center.x += 10;            break;        default:            break;    }    //更新center    self.photoButton.center = center;}//调整大小- (IBAction) size:(UIButton *) sender{    //取出bounds,调整bounds,图片是以自己中心进行缩放    CGRect bounds = self.photoButton.bounds;    //调整bounds    switch (sender.tag) {        case 5:            bounds.size.width -= 10;            bounds.size.height -= 10;            break;        case 6:            bounds.size.width += 10;            bounds.size.height += 10;            break;        default:            break;    }    //更新bounds    self.photoButton.bounds = bounds;}@end

方法三:利用tag,transform属性,使用开关语句和相关的C函数,对图片的位置和大小进行更改

#import "ViewController.h"@interface ViewController ()//私有属性@property (nonatomic,weak) IBOutlet UIButton* photoButton;//调整位置- (IBAction) move:(UIButton *) sender;//调整大小- (IBAction) size:(UIButton *) sender;//旋转图片-(IBAction) rotate:(UIButton *)sender;@end@implementation ViewController//调整位置- (IBAction) move:(UIButton *) sender{    //设置移动距离    CGPoint moveTo = {0,0};    switch (sender.tag) {        case 1:            moveTo.y -= 10;            break;        case 2:            moveTo.y += 10;            break;        case 3:            moveTo.x -= 10;            break;        case 4:            moveTo.x += 10;            break;        default:            break;    }    //移动函数CGAffineTransformTranslate(CGAffineTransform t,CGFloat tx, CGFloat ty);    //参数t,表示在谁的基础上移动    //参数tx,表示x轴的值,正数向上移动    //参数ty,表示x轴的值,负数向下移动    self.photoButton.transform  =  CGAffineTransformTranslate(self.photoButton.transform, moveTo.x, moveTo.y);}//调整大小- (IBAction) size:(UIButton *) sender{    //设置缩放倍数    CGPoint  scale = {0,0};    switch (sender.tag) {        case 5:            scale.x = 1.2;            scale.y = 1.2;            break;        case 6:            scale.x = 0.8;            scale.y = 0.8;            break;        default:            break;    }    //缩放函数CGAffineTransformScale(CGAffineTransform t,CGFloat sx, CGFloat sy);    //参数t,表示在谁的基础上缩放    //参数sx,表示x轴的缩放倍数,小于1缩小    //参数sy,表示x轴的缩放倍数,大于1放大    self.photoButton.transform = CGAffineTransformScale(self.photoButton.transform,scale.x  ,scale.y);}//旋转图片-(IBAction) rotate:(UIButton *)sender{    //设置弧度数    CGFloat angle = 0.25;    if (sender.tag == 7) {        angle = -0.25;    }    //旋转函数CGAffineTransformRotate(CGAffineTransform t,CGFloat angle);    //参数t,表示在谁的基础上旋转    //参数angle,表示旋转的弧度数,小于0逆时针旋转,大于0顺时针旋转    self.photoButton.transform =  CGAffineTransformRotate(self.photoButton.transform,angle);    }@end

方法四:不拖控件,用代码创建控件,实现对图片的移动

#import "ViewController.h"@interface ViewController ()//创建私有的属性@property (nonatomic,weak) UIButton* photoButton;@end@implementation ViewController//创建按钮-(UIButton*) buildButtonWhitX:(CGFloat) x Y:(CGFloat) y width:(CGFloat) w height:(CGFloat) h normal:(UIImage*) Image1 Highlighted:(UIImage*) Image2{    //创建按钮    UIButton* button = [[UIButton alloc]init];    //把按钮加入父控件    [self.view addSubview:button];    //设置尺寸    button.frame = CGRectMake(x, y, w, h);    //设置普通状态下的背景图片    [button setBackgroundImage:Image1 forState:UIControlStateNormal];    //设置普通状态下的背景图片    [button setBackgroundImage:Image2 forState:UIControlStateHighlighted];    return button;}- (void)viewDidLoad {    [super viewDidLoad];        //创建按钮并赋值给属性photoButton    self.photoButton = [self buildButtonWhitX:130 Y:100 width:100 height:100 normal:[UIImage imageNamed:@"btn_01"] Highlighted:[UIImage imageNamed:@"btn_02"]];        //创建upButton    UIButton* upButton = [self buildButtonWhitX:164 Y:321 width:42 height:30 normal:[UIImage imageNamed:@"top_normal"] Highlighted:[UIImage imageNamed:@"top_highlighted"]];    //upButton按钮执行up方法    [upButton addTarget:self action:@selector(up) forControlEvents:UIControlEventTouchUpInside];        //创建downButton    UIButton* downButton = [self buildButtonWhitX:164 Y:396 width:42 height:30 normal:[UIImage imageNamed:@"bottom_normal"] Highlighted:[UIImage imageNamed:@"bottom_highlighted"]];    //downButton按钮执行down方法    [downButton addTarget:self action:@selector(down) forControlEvents:UIControlEventTouchUpInside];        //创建leftButton    UIButton* leftButton = [self buildButtonWhitX:121 Y:358 width:46 height:30 normal:[UIImage imageNamed:@"left_normal"] Highlighted:[UIImage imageNamed:@"left_highlighted"]];    //leftButton按钮执行left方法    [leftButton addTarget:self action:@selector(left) forControlEvents:UIControlEventTouchUpInside];        //创建rightButton    UIButton* rightButton = [self buildButtonWhitX:205 Y:358 width:46 height:30 normal:[UIImage imageNamed:@"right_normal"] Highlighted:[UIImage imageNamed:@"right_highlighted"]];    //rightButton按钮执行right方法    [rightButton addTarget:self action:@selector(right) forControlEvents:UIControlEventTouchUpInside];}//调整frame-(void)frameX:(CGFloat)x Y:(CGFloat)y height:(CGFloat)h width:(CGFloat)w {    //取出button的frame    CGRect frame = self.photoButton.frame;    //调整frame    frame.origin.x = frame.origin.x + x;    frame.origin.y = frame.origin.y + y;    frame.size.height = frame.size.height + h;    frame.size.width = frame.size.width + w;    //更新button的frame    self.photoButton.frame = frame;}//向上移动-(void)up{    [self frameX:0 Y:-10 height:0 width:0 ];}//向下移动-(void)down{    [self frameX:0 Y:+10 height:0 width:0 ];}//向左移动-(void)left{    [self frameX:-10 Y:0 height:0 width:0];}//向右移动-(void)right{    [self frameX:+10 Y:0 height:0 width:0];}//缩小-(void)small{    [self frameX:0 Y:0 height:-10 width:-10];}//放大-(void)big{    [self frameX:0 Y:0 height:+10 width:+10];}@end


0 0
原创粉丝点击