Articles Tagged 'UIView'

Objective-C: Addendum on notifications and delegates

Answer to the question of ILeW with an article about what the real, attaching such as notifications and delegates work. Using a pattern we see first how the delegate pattern:

Delegate

An object in search of a delegate

Continued ...

Very short snippet: the UIView to display an image

Working with Interface Builder can be spontaneously use an object UIImageView to set the background of our view controller. Alternatively, you can delete this object, thus saving a little 'memory, and take advantage of the view associated with the view controller. Just plug in our view controller:

Continued ...

CaptureView: capture a UIView

o UIImageView ) in modo rapido. CaptureView allows you to clone, as an image, any view ( UIView or UIImageView ) quickly. (quindi visualizzato). What you get is an object CaptureView that can be used as a normal UIView (then displayed). Capturing the "screen", or any portion thereof, may be useful in many cases. Once the new "image", it can be manipulated without preoccparsi of its content.
Often you have multiple nested view and Carry out or processing effects on the mother's view that contains them all is not always an optimal solution.

Continued ...

Objective-C: handle multiple animations with a single setAnimationDidStopSelector

è estremamente versatile, come già visto in setAnimationDidStopSelector: usi diversi ed avanzati . The setting of the end of animation, executed with setAnimationDidStopSelector , a class of UIView is extremely versatile, as seen in setAnimationDidStopSelector: different uses and advanced .
Let me now show you a technique very convenient when we have multiple animations and / or want to manage the queues (queue) of animations.

Continued ...

Objective-C: how to rotate an image in 3D space

), un oggetto di più basso livello con innumerevoli potenzialità. The class UIView displays a pointer to the layer ( CALayer ), an object of the lower level with vast potential. The layer provides a variety of methods and properties related to displaying the contents of the view. For example, you can manipulate the contents of a view in three dimensional space. In ' sample carryover is interesting to note that the code is simple, and everything is created using Interface Builder:

Continued ...

Customize the sections in a UITableView Grouped

potremmo aver necessità di personalizzare la grafica dei titoli delle sezioni, come California o New York dell'esempio qui sotto. When we use a UITableView style Grouped we may need to customize the layout of section titles, such as California or New York the example below.

UITableView

To do so, please use the following code, placing it in the delegate, ie in the class that responds to the protocol UITableViewDelegate :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/ / Returns View my custom, in this case an object
/ / Type UILabel
UIView * ) tableView : ( UITableView * ) tableView - (UIView *) tableView: (UITableView *) tableView
NSInteger ) section { viewForHeaderInSection: (NSInteger) section {

[ [ [ UILabel alloc ] initWithFrame : CGRectZero ] autorelease ] ; UILabel * label = [[[UILabel alloc] initWithFrame: CGRectZero] autorelease];
UIFont boldSystemFontOfSize : 20 ] ; label.font = [UIFont boldSystemFontOfSize: 20];
label.textAlignment = UITextAlignmentCenter;
UIColor blackColor ] ; label.shadowColor = [UIColor blackColor];
1 , 1 ) ; label.shadowOffset CGSizeMake = (1, 1);
"Sezione" ; // Sostituire con un array come al solito Label.Text @ = "Section", / / Replace with an array as usual
UIColor whiteColor ] ; label.textColor = [UIColor whiteColor];
UIColor clearColor ] ; label.backgroundColor = [UIColor clearColor];
; label.opaque = NO;

return label;
}
/ / We must also support this message does not work otherwise
CGFloat ) tableView : ( UITableView * ) tableView - (CGFloat) tableView: (UITableView *) tableView
NSInteger ) section { heightForHeaderInSection: (NSInteger) section {
; return 44;
}

It is also important to include heightForHeaderInSection , will not work.

Notes of interest

o UIImageView , ho utilizzato per inizializzare il frame CGRectZero che corrisponde a CGRectMake(0,0,0,0) . In the creation of our UILabel , which might also be wanting a more complex object like a UIView or UIImageView , I used to initialize the frame CGRectZero corresponding to CGRectMake(0,0,0,0) .

Continued ...

setAnimationDidStopSelector: different uses and advanced

In most cases, or because we are used or because we have seen in tutorials and in some texts, we use the setAnimationDidStopSelector in this manner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
nil context : NULL ] ; [UIView beginAnimations: nil context: NULL];
1.5 ] ; [UIView setAnimationDuration: 1.5];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self ] ; [UIView setAnimationDelegate: self];
@selector ( removeView ) ] ; [UIView setAnimationDidStopSelector: @ selector (removeView)];

; myView.alpha = 0;

; [UIView commitAnimations];

/ /

void ) removeView { - (Void) {removeView
; [MyView removeFromSuperview];
}

come delegato e tramite la setAnimationDidStopSelector gli invia un messaggio removeView quando l'animazione è terminata. In the code above the setAnimationDelegate set self as delegate and through setAnimationDidStopSelector sends a message removeView when the animation is finished. The code itself is correct, however, makes use of a message definition ( removeView ) that could be omitted. Now, here is the same code, with the same effect, without the message removeView :

1
2
3
4
5
6
7
8
9
nil context : NULL ] ; [UIView beginAnimations: nil context: NULL];
1.5 ] ; [UIView setAnimationDuration: 1.5];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
myView ] ; [UIView setAnimationDelegate: myView];
@selector ( removeFromSuperview ) ] ; [UIView setAnimationDidStopSelector: @ selector (removeFromSuperview)];

; myView.alpha = 0;

; [UIView commitAnimations];

! The interesting thing about this approach is that myView could be a subclass of UIView ! It may therefore be a custom class with our own messages and, as stated, easily callable from setAnimationDidStopSelector . In addition, the setAnimationDidStopSelector selectors agree with parameters:

1
2
3
4
5
6
7
8
9
nil context : NULL ] ; [UIView beginAnimations: nil context: NULL];
1.5 ] ; [UIView setAnimationDuration: 1.5];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
myView ] ; [UIView setAnimationDelegate: myView];
@selector ( myMessage : param1 : ) ] ; [UIView setAnimationDidStopSelector: @ selector (myMessage: param1:)];

; myView.alpha = 0;

; [UIView commitAnimations];

This example can be extended to all cases here where we set a delegate, Atro is not a pointer to an instance of any object.

Continued ...

iPhone SecondApp: Guess the number - Part 2

As mentioned FirstApp iPhone: Guess the number - Part 1 we see how to make an application for the Apple iPhone without using Interface Builder! Indeed, we will eliminate physical files created by Interface Builder, Xcode wizard. At the end of this post, then, we have an application identical in all respects, to that achieved in the first part, with the difference that we will achieve all our visual components, including the main Window, completely to code.

The application already made, if you just want to download, is available on my Google Code repository:

I would like to point out immediately as the ZIP of this example weighing less than the last time! :)

We create the project

We start by creating our project SecondApp (to distinguish it from FirstApp), although this time choose Window-based Application:

newproject

Now let's delete everything related to Interface Builder. : eliminatelo anche dal file system, quindi selezionate Also Move to Trash . Delete the file MainWindow.xib , located in the folder Resources : also delete it from the file system, then select Also Move to Trash. nella casella Main nib file base name : Select then the file SecondoApp-info.plist and delete the reference to the MainWindow nib box Main base file name:

deleteib

At this point we no longer have any Windows, at least through Interface Builder. , e modifichiamo la funzione main() in questo modo: So we open the file main.m , situated in Other Sources , and modify the function main() like this:

1
2
3
4
5
6
7
8
9
10
11
int argc, char * argv [ ] ) { int main (int argc, char * argv []) {

pool = [ [ NSAutoreleasePool alloc ] init ] ; NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];

/ / By removing the file. XIB we missed the point
/ / Delegate to the JPA, then pass it to "hand"
UIApplicationMain ( argc, argv, nil , @ "SecondAppAppDelegate" ) ; int retVal = UIApplicationMain (argc, argv, nil, @ "SecondAppAppDelegate");

; [Pool release];
return retVal;
}

Open SecondAppAppDelegate.me then create the main Window in code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void ) applicationDidFinishLaunching : ( UIApplication * ) application { - (Void) applicationDidFinishLaunching: (UIApplication *) application {

/ / Get the screen size (320, 480)
UIScreen mainScreen ] applicationFrame ] ; CGRect windowRect = [[UIScreen Mainscreen] applicationFrame];
/ / Create a window - as we have done in Interface Builder bye bye
[ [ UIWindow alloc ] initWithFrame : windowRect ] ; MainWindow UIWindow * = [[UIWindow alloc] initWithFrame: windowRect];
/ / Set the background of the Window to yellow, to differentiate
/ / The previous application FirstApp
[ UIColor yellowColor ] ] ; [MainWindow setBackgroundColor: [UIColor yellowColor]];

mainWindow ] ; [Self setWindow: mainWindow];
; [Window makeKeyAndVisible];

; [MainWindow release];
}

You can already test the application, if you get a yellow window you did everything right!

, necessario solo se si usa Interface Builder. In the file SecondAppAppDelegate.h we can eliminate IBOutlet , only needed if you use Interface Builder. Also add here that our global variables, the last time we had entered in the Controller. Then modify the file SecondAppAppDelegate.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Import <UIKit/UIKit.h>

NSObject <UIApplicationDelegate> { @ Interface SecondAppAppDelegate: NSObject {<UIApplicationDelegate>
UIWindow * window;

UITextField * number;
UIButton * button;
numeroACaso int;
}

nonatomic, retain ) UIWindow * window; @ Property (nonatomic, retain) UIWindow * window;

void ) controllaNumero; - (Void) getNumber;

@ End

, in quanto non stiamo usando Interface Builder. Again we have prepared the method definition controllaNumero , like last time, but we have eliminated the indication IBAction , because you are not using Interface Builder.

We build the interface from code

It 'time to create code using all components of our interface. ed inseriamo il seguente codice: Let's go back to the file SecondAppAppDelegate.m , posizioniamoci before [mainWindow release] and insert the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/ / Create the title bar
[ [ UINavigationBar alloc ] initWithFrame : CGRectMake ( 0.0 , 0.0 , 320.0 , 44.0 ) ] ; UINavigationBar myNavigationBar * = [[UINavigationBar alloc] initWithFrame: CGRectMake (0.0, 0.0, 320.0, 44.0)];
myNavigationBar.barStyle = UIBarStyleDefault;
[ [ UINavigationItem alloc ] initWithTitle : @ "Indovina un numero" ] ; UINavigationItem navigationItem * = [[UINavigationItem alloc] initWithTitle: @ "Guess a number"];
navigationItem animated : NO ] ; [MyNavigationBar pushNavigationItem: navigationItem animated: NO];
myNavigationBar ] ; [Window addSubview: myNavigationBar];

/ / Create the label
[ [ UILabel alloc ] initWithFrame : CGRectMake ( 10 , 50 , 300 , 80 ) ] ; UILabel myLabel * = [[UILabel alloc] initWithFrame: CGRectMake (10, 50, 300, 80)];
UIColor clearColor ] ; myLabel.backgroundColor = [UIColor clearColor];
; myLabel.numberOfLines = 2;
"iPhone ha pensato un numero da 1 a 10, prova ad indovinarlo?" ; myLabel.text = @ "iPhone has designed a number from 1 to 10, try to guess?"
myLabel ] ; [Window addSubview: myLabel];

/ / Create text input
UITextField alloc ] initWithFrame : CGRectMake ( 10 , 120 , 300 , 30 ) ] ; number = [[UITextField alloc] initWithFrame: CGRectMake (10, 120, 300, 30)];
numero.borderStyle = UITextBorderStyleRoundedRect;
numero.textAlignment = UITextAlignmentCenter;
numero.keyboardType = UIKeyboardTypeNumberPad;
"Inserisci il numero" ; numero.placeholder = @ "Enter the number";
numero ] ; [Window addSubview: number];

/ / Create the button
UIButton buttonWithType : UIButtonTypeRoundedRect ] ; button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
10 , 180 , 300 , 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30);
@ "Premi qui" forState : UIControlStateNormal ] ; [Button setTitle: @ "Click Here" Forst: UIControlStateNormal];
self action : @selector ( controllaNumero ) forControlEvents : UIControlEventTouchUpInside ] ; [AddTarget button: self action: @ selector (getNumber) forControlEvents: UIControlEventTouchUpInside];
bottone ] ; [Window addSubview: button];

; [MyLabel release];
; [NavigationItem release];
; [MyNavigationBar release];

della scorsa volta, subito dopo il [mainWindow release]; inseriamo: Since the ' applicationDidFinishLaunching corresponds to viewDidLoad as last time, just after the [mainWindow release]; insert:

1
+ arc4random ( ) % 10 ; numeroACaso arc4random = 1 + () 10%;

Now we can do is implement the method controllaNumero , which will be identical (apart from the prototype) to that used last time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void ) controllaNumero { - (Void) {getNumber
"Premuto bottone di controlla numero" ) ; NSLog (@ "Pressed button control number");
[ numero.text integerValue ] ; numeroInserito = int [numero.text integerValue];
message; NSString * message;
"Il numero inserito è %d" , numeroInserito ) ; NSLog (@ "The number entered is% d", numeroInserito);
numeroInserito <numeroACaso ) { if (numeroInserito <numeroACaso) {
"Troppo basso..." ; message = @ "Too low ...";
( numeroInserito> numeroACaso ) { } Else if (numeroInserito> numeroACaso) {
"Troppo alto..." ; message = @ "Too high ...";
( numeroInserito == numeroACaso ) { } Else if (numeroInserito numeroACaso ==) {
"Bravo hai indovinato" ; message = @ "Bravo, you guessed it";
+ arc4random ( ) % 10 ; numeroACaso arc4random = 1 + () 10%;
"Numero pensato %d" , numeroACaso ) ; NSLog (@ "Number% d thought," numeroACaso);
}
[ [ UIAlertView alloc ] UIAlertView alertMessaggio * = [[UIAlertView alloc]
"Responso" initWithTitle: @ "Response"
message: message
delegate: nil
"OK" cancelButtonTitle: @ "OK"
] ; otherButtonTitles: nil];
; [AlertMessaggio show];
; [AlertMessaggio release];
"" ; numero.text @ = "";
}

We're done!

Conclusions and considerations

, proprio perchè volevo lasciarlo il più semplice e snello possibile e, anche, per dimostrare che non sono elementi sempre necessari. This example does not make direct use of a UIView or UIViewController , just because I wanted to leave it as simple and streamlined as possible and, also, to show that the elements are not always necessary. However insert objects directly in the window can have some sense in this example and other sporadic contexts. portano comunque benefici in tantissimi altri casi, ein alcuni sono praticamente indispensabili; come avremo modo di vedere in futuro. The use of UIView and UIViewController bring benefits in many other cases, however, some are virtually indispensable ein, as we will see in the future.

Continued ...

iPhone FirstApp: Guess the number - Part 1

The first source that I rose in my hands was written in Basic and consisted of a few lines of code, for enlightening me. It was a simple game that generates a random number from 1 to 10 and, through keyboard input, verify that the number entered was Maggioni, less than or equal to the random number. Despite its rough simplicity remains, for me, one of the best examples - simple, fun and practical - to explain to those who do not know anything about programming what is meant actually for "computer program". So I decided to propose it for Apple iPhone, maybe will help someone ...

Continued ...


Stop SOPA