iOS Development Basics-Handwriting controls, Frame,center and bounds properties

Source: Internet
Author: User
Tags uicontrol

iOS Development UI Basics-handwriting controls, Frame,center and Bounds properties
first, the handwriting control
1. Steps for Handwriting control
(1) Create a control object with the appropriate control class
(2) Set various properties of the control
(3) Adding controls to the view
(4) If the control is a button, you also need to consider the control's click event, etc.
(5) Note: The relationship between view contollor and view
2. Attention points
In OC Development, all operations in storyboard can be implemented by code, and programmers must be proficient in the ability of the code layout interface!
The sample code for setting the control listener method is as follows:
[Btn addtarget:self Action: @selector (click:) forcontrolevents:uicontroleventtouchupinside];
Tips:
The 1> Addtarget method is defined in the Uicontrol class, which means that you can add a listening method to all objects that inherit from the Uicontrol class
The first parameter of the 2> listener method is the object itself
The second parameter of the 3> listener method is the event that listens to the control
3. Code examples

1//1. Creating a Button object using a class
2//UIButton *headbtn=[[uibutton alloc] initwithframe:cgrectmake (100, 100, 100, 100)];
3//Set button object to custom type
4 UIButton *headbtn=[uibutton Buttonwithtype:uibuttontypecustom];
5
6//2. Setting individual properties of an object
7//(1) Location and other common property settings
8 Headbtn.frame=cgrectmake (100, 100, 100, 100);
9
10//(2) Set the properties of the button in the normal state
[Headbtn setbackgroundimage:[uiimage imagenamed:@ "I"] forstate:uicontrolstatenormal];
[headbtn settitle:@] dot me! [Forstate:uicontrolstatenormal];
[Headbtn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];
14
15//(3) Set the properties of the button in the highlighted state
[Headbtn setbackgroundimage:[uiimage imagenamed:@ "a"] forstate:uicontrolstatehighlighted];
[Headbtn settitle:@] "forstate:uicontrolstatehighlighted";
[Headbtn Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatehighlighted];
19
20//3. Add the object to the view to show it
[Self.view ADDSUBVIEW:HEADBTN];
22//Attention POINT!
SELF.HEADIMAGEVIEW=HEADBTN;

Second,Frame,center and Bounds Properties
1.frame, center, and bounds properties
Frame: Controlling Position and size
Center: Control Position (center point)
Bounds: Control size (in its own upper left corner as the origin)
2. Attention points
(1) The position of the control can be modified by the following properties
Frame.origin
Center
(2) The size of the control can be modified by the following properties
Frame.size
Bounds.size
3. Code examples
A program (frame, center, and bounds properties) that controls how the picture is shifted up or down, scaled

1//
2//YYVIEWCONTROLLER.M
3//01-Practice using the button's Frame and center properties
4//
5//Created by Apple on 14-5-21.
6//Copyright (c) 2014 itcase. All rights reserved.
7//
8
9 #import "YYViewController.h"
10
11//Private extension
@interface Yyviewcontroller ()
13
@property (nonatomic,weak) iboutlet UIButton *headimageview;
@end
16
@implementation Yyviewcontroller
18
19//enumeration type, starting from 1
typedef enum
21 {
Ktopbtntag=1,
Kdownbtntag,
Krightbtntag,
Kleftbtntag
}btntag;
27
//viewdidload is the method that is called after the view is loaded, typically performing the initialization of the view controller in this method
-(void) viewdidload
30 {
31
32//In the Viewdidload method, do not forget to invoke the method implementation of the parent class
[Super Viewdidload];
34
35
36//Handwriting Control code
37//One, write a button control with a picture above it
38
39//1. Creating a Button object using a class
+//UIButton *headbtn=[[uibutton alloc] initwithframe:cgrectmake (100, 100, 100, 100)];
41//Set button object to custom type
UIButton *headbtn=[uibutton Buttonwithtype:uibuttontypecustom];
43
44//2. Setting individual properties of an object
45//(1) Location and other common property settings
Headbtn.frame=cgrectmake (100, 100, 100, 100);
47
48//(2) Set the properties of the button in the normal state
[Headbtn setbackgroundimage:[uiimage imagenamed:@ "I"] forstate:uicontrolstatenormal];
[headbtn settitle:@] dot me! [Forstate:uicontrolstatenormal];
Wuyi [headbtn Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];
52
53//(3) Set the properties of the button in the highlighted state
[Headbtn setbackgroundimage:[uiimage imagenamed:@ "a"] forstate:uicontrolstatehighlighted];
[Headbtn settitle:@] is OK ~ "forstate:uicontrolstatehighlighted";
[Headbtn Settitlecolor:[uicolor Bluecolor] forstate:uicontrolstatehighlighted];
57
58//3. Add the object to the view to show it
[Self.view ADDSUBVIEW:HEADBTN];
60//Attention point!
SELF.HEADIMAGEVIEW=HEADBTN;
62
63
64//Two, write four control the picture and move the direction of the button control
65
66/**================ up Button =====================*/
67//1. Creating a Button Object
UIButton *topbtn=[uibutton Buttonwithtype:uibuttontypecustom];
69
70//2. Setting properties of an object
Topbtn.frame=cgrectmake (100, 250, 40, 40);
[Topbtn setbackgroundimage:[uiimage imagenamed:@ "Top_normal"] forstate:uicontrolstatenormal];
[Topbtn setbackgroundimage:[uiimage imagenamed:@ "top_highlighted"] forstate:uicontrolstatehighlighted];
[Topbtn settag:1];
75//3. Adding controls to the view
[Self.view ADDSUBVIEW:TOPBTN];
77
78//4. Click Control events for buttons
[Topbtn addtarget:self Action: @selector (Click:) forcontrolevents:uicontroleventtouchupinside];
80
81
82/**================ down button =====================*/
83//1. Creating a Button Object
UIButton *downbtn=[uibutton Buttonwithtype:uibuttontypecustom];
85//2. Setting properties of an object
Downbtn.frame=cgrectmake (100, 350, 40, 40);
[Downbtn setbackgroundimage:[uiimage imagenamed:@ "Bottom_normal"] forstate:uicontrolstatenormal];
[Downbtn setbackgroundimage:[uiimage imagenamed:@ "bottom_highlighted"] forstate:uicontrolstatehighlighted];
[Downbtn Settag:2];
90//3. Adding Controls to the view
[Self.view ADDSUBVIEW:DOWNBTN];
92
93//4. Click Control events for buttons
94 [Downbtn addtarget:self Action: @selector (Click:) forcontrolevents:uicontroleventtouchupinside];
95
96
97/**================ left Button =====================*/
98//1. Creating a Button Object
UIButton *leftbtn=[uibutton Buttonwithtype:uibuttontypecustom];
100//2. Setting properties of an object
101 Leftbtn.frame=cgrectmake (50, 300, 40, 40);
102 [leftbtn setbackgroundimage:[uiimage imagenamed:@ "Left_normal"] forstate:uicontrolstatenormal];
103 [leftbtn setbackgroundimage:[uiimage imagenamed:@ "left_highlighted"] forstate:uicontrolstatehighlighted];
104 [Leftbtn Settag:4];
105//3. Adding controls to the view
106 [Self.view ADDSUBVIEW:LEFTBTN];
107
108//4. Click Control events for buttons
109 [leftbtn addtarget:self Action: @selector (Click:) forcontrolevents:uicontroleventtouchupinside];
110
111
112
113/**================ Right Button =====================*/
114//1. Creating a Button Object
UIButton *rightbtn=[uibutton Buttonwithtype:uibuttontypecustom];
116//2. Setting properties of an object
117 Rightbtn.frame=cgrectmake (150, 300, 40, 40);
118 [rightbtn setbackgroundimage:[uiimage imagenamed:@ "Right_normal"] forstate:uicontrolstatenormal];
119 [Rightbtn setbackgroundimage:[uiimage imagenamed:@ "right_highlighted"] forstate:uicontrolstatehighlighted];
[Rightbtn Settag:3];
121//3. Adding controls to the view
122 [Self.view ADDSUBVIEW:RIGHTBTN];
123
124//4. Click Control events for buttons
[Rightbtn addtarget:self Action: @selector (Click:) forcontrolevents:uicontroleventtouchupinside];
126
127//Three, write two zoom buttons
128/**================ Enlarged Button =====================*/
129//1. Creating objects
UIButton *plusbtn=[uibutton Buttonwithtype:uibuttontypecustom];
131//2. Setting properties
Plusbtn.frame=cgrectmake (75, 400, 40, 40);
133 [plusbtn setbackgroundimage:[uiimage imagenamed:@ "Plus_normal"] forstate:uicontrolstatenormal];
134 [plusbtn setbackgroundimage:[uiimage imagenamed:@ "plus_highlighted"] forstate:uicontrolstatehighlighted];
135 [plusbtn settag:1];
136//3. Adding to a view
137 [Self.view ADDSUBVIEW:PLUSBTN];
138//4. Click events
139 [Plusbtn addtarget:self Action: @selector (Zoom:) forcontrolevents:uicontroleventtouchupinside];
140
141
142/**================ Zoom-out button =====================*/
143 UIButton *minusbtn=[uibutton Buttonwithtype:uibuttontypecustom];
144 Minusbtn.frame=cgrectmake (125, 400, 40, 40);
145 [minusbtn setbackgroundimage:[uiimage imagenamed:@ "Minus_normal"] forstate:uicontrolstatenormal];
146 [minusbtn setbackgroundimage:[uiimage imagenamed:@ "minus_highlighted"] forstate:uicontrolstatehighlighted];
147 [minusbtn settag:0];
148 [Self.view ADDSUBVIEW:MINUSBTN];
149 [minusbtn addtarget:self Action: @selector (Zoom:) forcontrolevents:uicontroleventtouchupinside];
150}
151
152//control the direction of multiple buttons to call the same method
153-(void) Click: (UIButton *) button
154 {
155
156//Practice using the Frame property
157//cgrect Frame=self.headimageview.frame;
158
159/** Note that if you control the position of the two properties frame and center at the same time, there will be a very interesting effect, pay attention to analysis */
160//Practice using the Center property
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//found a bug, before the problem is because less write break, resulting in their sequential execution, sorry
171//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 animation effect
182 [UIView Beginanimations:nil Context:nil];
183 Self.headimageview.center=center;
184//Set time
185 [UIView setanimationduration:2.0];
186 [UIView commitanimations];
187 NSLog (@ "Move!");
188
189}
-(void) Zoom: (UIButton *) btn
191 {
192//Use frame, at its own upper left corner (own origin) as the Origin point
193//CGRect frame=self.headimageview.frame;
194//if (Btn.tag) {
195//frame.size.height+=30;
196//frame.size.width+=30;
197//}
198//Else
199//{
//frame.size.width-=50;
201//FRAME.SIZE.HEIGHT-=50;
202//}
203//Self.headimageview.frame=frame;
204
205
206//Use bounds to zoom to center point origin
207 CGRect bounds = self.headImageView.bounds;
208 if (Btn.tag) {
209 bounds.size.height+=30;
bounds.size.width+=30;
211}
212 Else
213 {
214 bounds.size.height-=50;
215 bounds.size.width-=50;
216}
217
218//Set Animation
219 [UIView Beginanimations:nil Context:nil];
Self.headimageview.bounds=bounds;
221 [UIView setanimationduration:2.0];
222 [UIView commitanimations];
223}
224 @end
Implementation results:

Three, simple animation effect
A brief introduction to the end-to-end animation effect
(1) Start animation
(2) Setting animation-related time, etc.
(3) Action to participate in animation
(4) Submit animation
Note: Implement code reference above code


iOS Development Basics-Handwriting controls, Frame,center and bounds properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.