I. Handwriting controls 1. step 1: Create a widget object using the corresponding spatial class (2) set various properties of the widget (3) Add the widget to the view (4) for buttons and other controls, you also need to consider the click events of the controls. (5) Note: Relationship between View Contollor and view 2. note that in OC development, all operations in Storyboard can be implemented through code. programmers must be familiar with the code layout interface! The sample code for setting the control listening method is as follows: [btn addTarget: self action: @ selector (click :) forControlEvents: UIControlEventTouchUpInside]; prompt: 1> the addTarget method is defined in the UIControl class, this means that you can add a listening method to all objects inherited from the UIControl class. 2. The first parameter of the listening method is the object itself. 3. The second parameter of the listening method is the event of the listening control. 3. sample Code copy code 1 // 1. use the class to create a button object 2 // UIButton * headbtn = [[UIButton alloc] initWithFrame: CGRectMake (100,100,100,100)]; 3 // set the button object to custom type 4 UIButton * headbtn = [UIButton buttonWithType: UIBut TonTypeCustom]; 5 6 // 2. set all attributes of the object 7 // (1) position and other general attributes 8 headbtn. frame = CGRectMake (100,100,100,100); 9 10 // (2) set the attributes of the buttons in normal state 11 [headbtn setBackgroundImage: [UIImage imageNamed: @ "I"] forState: UIControlStateNormal]; 12 [headbtn setTitle: @ "Click me! "ForState: UIControlStateNormal]; 13 [headbtn setTitleColor: [UIColor redColor] forState: UIControlStateNormal]; 14 15 // (3) set the property of the button in the highlighted state 16 [headbtn setBackgroundImage: [UIImage imageNamed: @ "a"] forState: UIControlStateHighlighted]; 17 [headbtn setTitle: @ "OK ~ "ForState: UIControlStateHighlighted]; 18 [headbtn setTitleColor: [UIColor blueColor] forState: UIControlStateHighlighted]; 19 20 // 3. add the object to the view to display 21 [self. view addSubview: headbtn]; 22 // note! 23 self. headImageView = headbtn; copy Code 2, frame, center, and bounds attributes 1. frame, center, and bounds attributes frame: control position and size center: control position (center point) bounds: control the size (based on your own upper left corner as the origin) 2. note: (1) You can use the following attributes to modify the widget's position frame. origin center (2) You can modify the widget size frame using the following attributes. size bounds. size 3. code example: a program that controls the translation between the top, bottom, and left of an image. The scaled Program (frame, center, and bounds attributes) Copies code 1 // 2 // YYViewController. m 3 // 01-frame and center properties of the exercise button 4 // 5 // Created by apple on 14-5-21. 6 // Copyright (c) 2014 itcas E. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 // Private expansion 12 @ interface YYViewController () 13 14 @ property (nonatomic, weak) IBOutlet UIButton * headImageView; 15 @ end 16 17 @ implementation YYViewController 18 19 // Enumeration type, starting from 1 20 typedef enum 21 {22 ktopbtntag = 1, 23 kdownbtntag, 24 krightbtntag, 25 kleftbtntag 26} btntag; 27 28 // viewDidLoad is the method called after the view is loaded. In this method, initialize the View Controller 29-(voi D) viewDidLoad 30 {31 32 // In the viewDidLoad method, do not forget to call the method of the parent class to implement 33 [super viewDidLoad]; 34 35 36 // handwritten Control Code 37 // 1. Write a button control with an image 38 39 // 1 on it. use the class to create a button object 40 // UIButton * headbtn = [[UIButton alloc] initWithFrame: CGRectMake (100,100,100,100)]; 41 // set the button object to custom type 42 UIButton * headbtn = [UIButton buttonWithType: UIButtonTypeCustom]; 43 44 // 2. set the attributes of the object, such as 45 // (1) location, and other general attributes. Set 46 headbtn. frame = CGRectMake (100,100,100, 10 0); 47 48 // (2) set the attributes of buttons in normal state 49 [headbtn setBackgroundImage: [UIImage imageNamed: @ "I"] forState: UIControlStateNormal]; 50 [headbtn setTitle: @ "Click me! "ForState: UIControlStateNormal]; 51 [headbtn setTitleColor: [UIColor redColor] forState: UIControlStateNormal]; 52 53 // (3) set the property of the button in the highlighted State 54 [headbtn setBackgroundImage: [UIImage imageNamed: @ "a"] forState: UIControlStateHighlighted]; 55 [headbtn setTitle: @ "OK ~ "ForState: UIControlStateHighlighted]; 56 [headbtn setTitleColor: [UIColor blueColor] forState: UIControlStateHighlighted]; 57 58 // 3. add the object to the view to display 59 [self. view addSubview: headbtn]; 60 // note! 61 self. headImageView = headbtn; 62 63 64 // 2. Write the button control 65 66/*** ====================== up button =====================================*/67 // 1. create button object 68 UIButton * topbtn = [UIButton buttonWithType: UIButtonTypeCustom]; 69 70 // 2. set the object property 71 topbtn. frame = CGRectMake (100,250, 40, 40); 72 [topbtn setBackgroundImage: [UIImage imageNamed: @ "top_normal"] forState: UIControlStateNormal]; 73 [topbtn setBackgroundImage: [UIImage MageNamed: @ "top_highlighted"] forState: UIControlStateHighlighted]; 74 [topbtn setTag: 1]; 75 // 3. add the control to the view 76 [self. view addSubview: topbtn]; 77 78 // 4. click the control event 79 [topbtn addTarget: self action: @ selector (Click: forControlEvents: UIControlEventTouchUpInside]; 80 81 82/*** ======================== down button ====================== ======= */83 // 1. create a button object 84 UIButton * downbtn = [UIButton buttonWithType: UIButtonTypeCustom]; 85/ /2. set the property of the object 86 downbtn. frame = CGRectMake (100,350, 40, 40); 87 [downbtn setBackgroundImage: [UIImage imageNamed: @ "bottom_normal"] forState: UIControlStateNormal]; 88 [downbtn success: [UIImage imageNamed: @ "bottom_highlighted"] forState: UIControlStateHighlighted]; 89 [downbtn setTag: 2]; 90 // 3. add the control to the view. 91 [self. view addSubview: downbtn]; 92 93 // 4. click the control event 94 [downbtn addTarget: self action :@ Selector (Click :) forControlEvents: UIControlEventTouchUpInside]; 95 96 97/** ========================== left button ========== ========= */98 // 1. create a button object 99 UIButton * leftbtn = [UIButton buttonWithType: UIButtonTypeCustom]; 100 // 2. set the attribute of the object to 101 leftbtn. frame = CGRectMake (50,300, 40, 40); 102 [leftbtn setBackgroundImage: [UIImage imageNamed: @ "left_normal"] forState: UIControlStateNormal]; 103 [leftbtn restart: [UIImag E imageNamed: @ "left_highlighted"] forState: UIControlStateHighlighted]; 104 [leftbtn setTag: 4]; 105 // 3. add the control to the view 106 [self. view addSubview: leftbtn]; 107 108 // 4. click the control event 109 [leftbtn addTarget: self action: @ selector (Click :) forControlEvents: UIControlEventTouchUpInside]; 110 111 112 113/** ======================== right button ============ ======== */114 // 1. create button object 115 UIButton * rightbtn = [UIButton buttonWithType: UIButtonTy PeCustom]; 116 // 2. set the property of the object 117 rightbtn. frame = CGRectMake (150,300, 40, 40); 118 [rightbtn failed: [UIImage imageNamed: @ "right_normal"] forState: UIControlStateNormal]; 119 [rightbtn failed: [UIImage imageNamed: @ "right_highlighted"] forState: UIControlStateHighlighted]; 120 [rightbtn setTag: 3]; 121 // 3. add the control to the view 122 [self. view addSubview: rightbtn]; 123 124 // 4. click the control event 125 [rightbtn add Target: self action: @ selector (Click :) forControlEvents: UIControlEventTouchUpInside]; 126 127 // 3. Write two zoom buttons: 128/** ================================== ================= */129 // 1. create object 130 UIButton * plusbtn = [UIButton buttonWithType: UIButtonTypeCustom]; 131 // 2. set property 132 plusbtn. frame = CGRectMake (75,400, 40, 40); 133 [plusbtn setBackgroundImage: [UIImage imageNamed: @ "plus_normal"] forState: UIControlStateNormal]; 134 [plusbt N setBackgroundImage: [UIImage imageNamed: @ "plus_highlighted"] forState: UIControlStateHighlighted]; 135 [plusbtn setTag: 1]; 136 // 3. add it to view 137 [self. view addSubview: plusb tn]; 138 // 4. click Event 139 [plusbtn addTarget: self action: @ selector (Zoom :) forControlEvents: UIControlEventTouchUpInside]; 140 141 142/*** ========================== zoom out button ============ ======= */143 UIButton * minusbtn = [UIButton buttonWithType: UIButtonTypeCu Stom]; 144 minusbtn. frame = CGRectMake (125,400, 40, 40); 145 [minusbtn setBackgroundImage: [UIImage imageNamed: @ "minus_normal"] forState: UIControlStateNormal]; 146 [minusbtn success: [UIImage imageNamed: @ "minus_highlighted"] forState: UIControlStateHighlighted]; 147 [minusbtn setTag: 0]; 148 [self. view addSubview: minusbtn]; 149 [minusbtn addTarget: self action: @ selector (Zoom :) forControlEvents: UICo NtrolEventTouchUpInside]; 150} 151 152 // multiple buttons in the control direction call the same method 153-(void) Click :( UIButton *) button154 {155 156 // exercise using the frame attribute 157 // CGRect frame = self. headImageView. frame; 158 159/** Note: if the frame and center attributes of the control position are used at the same time, it will be very interesting, pay attention to analysis */160 // exercise using the center attribute 161 CGPoint center = self. headImageView. center; 162 switch (button. tag) {163 case ktopbtntag: 164 center. y-= 30; 165 break; 166 case kdownbtntag: 167 center. y + = 30; 168 Break; 169 case kleftbtntag: 170 // a bug was found. The previous problem was that less break was written, resulting in sequential execution. sorry171 // center. x = center. x-30; 172 center. x-= 50; 173 break; 174 case krightbtntag: 175 center. x + = 50; 176 break; 177} 178 179 // self. headImageView. frame = frame; 180 181 // set the animation effect at the beginning and end. 182 [UIView beginAnimations: nil context: nil]; 183 self. headImageView. center = center; 184 // set the time to 185 [UIView setAnimationDuration: 2.0]; 186 [UIView commitAnimation S]; 187 NSLog (@ "move! "); 188 189} 190-(void) Zoom :( UIButton *) btn191 {192 // use frame in the upper left corner of the page (your origin) is the origin 193 // CGRect frame = self. headImageView. frame; 194 // if (btn. tag) {195 // frame. size. height + = 30; 196 // frame. size. width + = 30; 197 //} 198 // else199 // {200 // frame. size. width-= 50; 201 // frame. size. height-= 50; 202 //} 203 // self. headImageView. frame = frame; 204 205 206 // use bounds to scale 207 CGRect bounds = self at the center point origin. headImageView. bounds; 208 if (btn. tag) {209 bounds. size. height + = 30; 210 bounds. size. width + = 30; 211} 212 else213 {214 bounds. size. height-= 50; 215 bounds. size. width-= 50; 216} 217 218 // set the first and last animation 219 [UIView beginAnimations: nil context: nil]; 220 self. headImageView. bounds = bounds; 221 [UIView setAnimationDuration: 2.0]; 222 [UIView commitAnimations]; 223} 224 @ end