现在的位置: 首页 > 综合 > 正文

IOS基础进阶-基础控件

2018年05月26日 ⁄ 综合 ⁄ 共 3030字 ⁄ 字号 评论关闭

UIView 中

@property(nonatomic) CGRect frame;-----不带星号*,是结构体

控件所在矩形框的位置和尺寸--以父控件的左上角为坐标原点

@property(nonatomic) CGRect bounds;

以自己左上角为坐标原点,所以bounds的x/y一般为0

@property(nonatomic) CGPoint center;

控件中点的位置--以父控件的左上角为坐标原点

@property(nonatomic) NSInteger tag;

控件的ID/标识,父控件可以通过tag来找到对应的自控件

@property(nonatomic,readonly) UIView *superView;

获得自己的父控件对象

@property(nonatomic,readonly,copy) NSArray *subViews;

获得所有子控件

后放入的控件在前面控件的上面一层,所以subViews用有存储顺序的NSArray来存储


UIButton:

1.normal :普通 default

2.highlighted:高亮

控件的上下左右移动,旋转,放大缩小:

#import <UIKit/UIKit.h>

@interface CHViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *btn;
- (IBAction)run:(id)sender;
- (IBAction)baserun:(id)sender;
- (IBAction)rotate:(id)sender;
- (IBAction)baseRotate:(id)sender;
- (IBAction)scale:(id)sender;
@end

//  CHViewController.m
//  buttonControl
//  1.当旋转的时候如果移动会有bug---暂时解决不了,以后会解决
//  Created by CH on 14-5-11.
//  Copyright (c) 2014年 ch. All rights reserved.
//

#import "CHViewController.h"
//将一个数抽出来三种做法:变量,宏,const
#define kDelta 50;//这样效率更高,不用分配存储控件给delta
//const int DELTA = 50;//c++写法

#pragma mark 控件移动
@implementation CHViewController

#pragma mark 控件上下左右移动
- (IBAction)baserun:(id)sender{//基本移动方式
    //OC语法规定,不允许直接修改某个对象结构体属性的成员
    //0.动画开始--保证需要动画的代码包含在这里面
    [UIView beginAnimations:nil context:nil];
    //设置动画参数
    [UIView setAnimationDuration:3.0];
    //1.先取出frame
    CGRect frame = _btn.frame;  //新分配一个临时内存存放frame
    //2.再修改y值
    frame.origin.y -=100;    //修改了新的frame,并没有修改原来的frame
    //3.再赋值回按钮的frame
    _btn.frame=frame;
    //4.动画提交
    [UIView commitAnimations];
}
- (IBAction)run:(id)sender {
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    CGRect frame = _btn.frame;  //新分配一个临时内存存放frame
    //int tag = _btn.tag;这种用法不对
    //sender.tag这种也不对,id类型的对象不能使用点语法,要使用getset方法
    int tag = [sender tag];
    //CGFloat kDelta = 50;//方式一:变量。方式二:写宏
    //宏:#define kDelta 50;//这样效率更高,不用分配存储控件给delta
    //NSLog(@"%d",tag);
    switch (tag) {
        case 1://上
            frame.origin.y -=kDelta;
            break;
        case 2://右
            frame.origin.x +=kDelta;
            break;
        case 3://下
            frame.origin.y +=kDelta;
            break;
        case 4://左
            frame.origin.x -=kDelta;
            break;
            
        default:
            break;
    }//修改了新的frame,并没有修改原来的frame
    _btn.frame=frame;
    [UIView commitAnimations];
}

#pragma mark 控件左旋转,右旋转
- (IBAction)baseRotate:(id)sender{//基本旋转方式
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    _btn.transform = CGAffineTransformRotate(_btn.transform, - M_PI_4);
    
    [UIView commitAnimations];
}
- (IBAction)rotate:(id)sender {//左旋转
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    int tag = [sender tag];
//    if (11 == tag) {//左旋转
//        _btn.transform = CGAffineTransformRotate(_btn.transform, - M_PI_4);
//    }else if(12 ==  tag){//右旋转
//        _btn.transform = CGAffineTransformRotate(_btn.transform, + M_PI_4);
//    }
    _btn.transform = CGAffineTransformRotate(_btn.transform, M_PI_4 * ((11==tag)?(-1):(1)));
    [UIView commitAnimations];

}

#pragma mark 控件放大缩小
- (IBAction)scale:(id)sender{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    int tag = [sender tag];
//    if(13 == tag){
//        _btn.transform = CGAffineTransformScale(_btn.transform, 1.2, 1.2);
//    }else if(14 == tag){
//        _btn.transform = CGAffineTransformScale(_btn.transform, 0.8, 0.8);
//    }
    CGFloat scale = (13==tag ? 1.2 : 0.8);
    _btn.transform = CGAffineTransformScale(_btn.transform, scale, scale);
    
    [UIView commitAnimations];
}
@end

抱歉!评论已关闭.