iOS开发之按钮的基本使用

实现功能:

点击向上的箭头,按钮图片向上,移动,点击向下的箭头,按钮图片向下移动

点击向左的箭头,按钮图片向左移动,点击向右的箭头,按钮图片向右移动,

点击加号图片放大,点击减号,图片缩小

 

 

 

 

第一步: 搭建界面,将控件分别连线

 

第二步: 将图片按钮连线

@property (weak, nonatomic) IBOutlet UIButton *headBtn;

 

 

第三步: 在每个按钮点击事件中实现向上,向下,向左,向右,放大,缩小的功能

 

//想上

- (IBAction)up:(id)sender {

//    NSLog(@"");

//    self.headBtn.frame.origin.y = self.headBtn.frame.origin.y - 10;

    //不能直接访问对象的结构体属性的成员变量

    //能够直接访问对象的结构体属性

//    self.headBtn.frame

    //1 取出对象的结构体属性frame 赋值给临时的变量

    CGRect tempFrame = self.headBtn.frame;

    //2 修改临时变量的值

//    tempFrame.origin.y = tempFrame.origin.y - 10;

    //

    tempFrame.origin.y -= 10;

    //3 用临时变量的值覆盖原来的值

    self.headBtn.frame = tempFrame;

    

}

//向下

- (IBAction)down:(id)sender {

//    NSLog(@"");

    //1 取出对象的结构体属性frame 赋值给临时的变量

    CGRect tempFrame = self.headBtn.frame;

    //2 修改临时变量的值

    //    tempFrame.origin.y = tempFrame.origin.y - 10;

    //

    tempFrame.origin.y += 10;

    //3 用临时变量的值覆盖原来的值

    self.headBtn.frame = tempFrame;

}

 

/**

 向左

 

 

 */

- (IBAction)left:(id)sender {

//    NSLog(@"");

    CGRect tempFrame = self.headBtn.frame;

    //2 修改临时变量的值

    //    tempFrame.origin.y = tempFrame.origin.y - 10;

    //

    tempFrame.origin.x -= 10;

    //3 用临时变量的值覆盖原来的值

    self.headBtn.frame = tempFrame;

}

 

/**

 向右

 

 @param sender <#sender description#>

 */

- (IBAction)right:(id)sender {

//    NSLog(@"");

    //1 取出对象的结构体属性frame 赋值给临时的变量

    CGRect tempFrame = self.headBtn.frame;

    //2 修改临时变量的值

    //    tempFrame.origin.y = tempFrame.origin.y - 10;

    //

    tempFrame.origin.x += 10;

    //3 用临时变量的值覆盖原来的值

    self.headBtn.frame = tempFrame;

}

 

 

 

/**

 放大

 

 @param sender <#sender description#>

 */

- (IBAction)big:(id)sender {

    

//    NSLog(@"");

    CGRect tempFrame = self.headBtn.frame;

    //2 修改临时变量的值

    //    tempFrame.origin.y = tempFrame.origin.y - 10;

    //

    tempFrame.size.width += 10;

    tempFrame.size.height +=10;

    //3 用临时变量的值覆盖原来的值

    self.headBtn.frame = tempFrame;

 

}

 

/**

 缩小

 

 @param sender <#sender description#>

 */

- (IBAction)small:(id)sender {

//   NSLog(@"");

    CGRect tempFrame = self.headBtn.frame;

    //2 修改临时变量的值

    //    tempFrame.origin.y = tempFrame.origin.y - 10;

    //

    tempFrame.size.width -= 10;

    tempFrame.size.height -=10;

    //3 用临时变量的值覆盖原来的值

    self.headBtn.frame = tempFrame;

    

}

@end

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2017-07-25 15:05  李洪强  阅读(295)  评论(0编辑  收藏  举报