IOS development-UIView Summary

Source: Internet
Author: User

If you want to call a method of a class, you can write this method from the NSObject class.

 

C code example mselector:
PerformSelector: withObject:
PerformSelector: withObject:

Actual call

 

C code [self defined mselector: @ selector (displayViews) withObject: nil afterDelay: 1.0f];

There are three methods:

 

C code // parent View
[Self. view superview]
// All subviews
[Self. view subviews]
// Its own window
Self. view. window

Loop the methods of all views under a view

 

C code NSArray * allSubviews (UIView * aView)
{
NSArray * results = [aView subviews];
For (UIView * eachView in [aView subviews])
{
NSArray * riz = allSubviews (eachView );
If (riz ){
Results = [results arrayByAddingObjectsFromArray: riz];
}
}
Return results;
}

Returns all views in an APPLICATION in a loop.

 

C code // Return all views throughout the application
NSArray * allApplicationViews ()
{
NSArray * results = [[UIApplication sharedApplication] windows];
For (UIWindow * window in [[UIApplication sharedApplication] windows])
{
NSArray * riz = allSubviews (window );
If (riz) results = [results arrayByAddingObjectsFromArray: riz];
}
Return results;
}
 
Find all parent views

 

C code // Return an array of parent views from the window down to the view
NSArray * pathToView (UIView * aView)
{
NSMutableArray * array = [NSMutableArray arrayWithObject: aView];
UIView * view = aView;
UIWindow * window = aView. window;
While (view! = Window)
{
View = [view superview];
[Array insertObject: view atIndex: 0];
}
Return array;
}

UIView provides a large number of management views

 

C code // Add a view to a view
AddSubview:
// Move a View to the front
BringSubviewToFront:
// Push a View to the back
SendSubviewToBack:
// Remove the view
RemoveFromSuperview
// Insert the view and specify the index
InsertSubview: atIndex:
// Insert a view above a view
InsertSubview: aboveSubview:
// Insert a view under a view
InsertSubview: belowSubview:
// Exchange views of Two-position Indexes
ExchangeSubviewAtIndex: withSubviewAtIndex:

View callback

 

C code // call after the view is added
(Void) didAddSubview :( UIView *) subview
// Called when the view is moved
(Void) didMoveToSuperview
// Call after the view is moved to the new WINDOW
(Void) didMoveToWindow
// Call after deleting the view
(Void) willRemoveSubview :( UIView *) subview
// Called before the view is moved
(Void) didMoveToSuperview :( UIView *) subview
// Called before the view is moved to the WINDOW
(Void) didMoveToWindow

Set tag and search view for UIView

 

C code myview. tag = 1001;
[Self. view viewwithtagag: 1001];
(UILable *) [self. view. window viewWithTag: 1001];

View geometric features

 

C code // framework
Struct CGPoint {
CGFloat x;
CGFloat y;
};
Typedef struct CGPoint;
 
/* Sizes .*/
 
Struct CGSize {
CGFloat width;
CGFloat height;
};
Typedef struct CGSize;
 
Struct CGRect {
CGPoint origin;
CGSize size;
};
Typedef struct CGRect;
 
 
 
CGRect rect = CGRectMake (0, 0, 320,480 );
UIView * view = [[UIView allow] initWithFrame: rect];
 
// Convert String to CGPoint such as @ "{3.0, 2.5}" {x, y}
CGPoint CGPointFromString (
NSString * string
);
 
// Convert String to CGRect @ "{3, 2}, {4, 5}" {x, y}, {w, h }}
CGRect CGRectFromString (
NSString * string
);
 
// Convert String to CGSize @ "{3.0, 2.5}" {w, h}
CGSize CGSizeFromString (
NSString * string
);
 
// Convert CGPoint to NSString
NSString * NSStringFromCGPoint (
CGPoint point
);
 
// Convert CGRect to NSString
NSString * NSStringFromCGRect (
CGRect rect
);
 
// Convert CGSize to NSString
NSString * NSStringFromCGSize (
CGSize size
);
 
// Modify a CGRect. Change the positive number in the center to indicate smaller (reduced) negative number to greater (enlarged)
CGRect CGRectInset (
CGRect rect,
CGFloat dx,
CGFloat dy
);
 
// Determine whether two rectangles are intersecting
Bool CGRectIntersectsRect (
CGRect rect1,
CGRect rect2
);
 
// The initial value is 0.
Const CGPoint CGPointZero;
Const CGRect CGRectZero;
Const CGSize CGSizeZero;
 
// Create a CGPoint
CGPoint CGPointMake (
CGFloat x,
CGFloat y
);
// Create CGRect
CGRect CGRectMake (
CGFloat x,
CGFloat y,
CGFloat width,
CGFloat height
);
// Create CGSize
CGSize CGSizeMake (
CGFloat width,
CGFloat height
);

Affine Transformation

 

C code CGAffineTransform form = CGAffineTransformMakeRotation (PI );
Myview. transform = form;

Restore

 

C code myview. transform = CGAffineTransformIdentity;

Directly set the center of the View

 

C code myview. center = CGPointMake (100,200 );

Center

 

C code CGRectGetMinX
CGRectGetMinY
// The median of X
CGRectGetMidX
// The median of Y
CGRectGetMidY
CGRectGetMaxX
CGRectGetMaxY

Timer

 

C code NSTime * timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target: self selector: @ selector (move :) userInfo: nil repeats: YES];

Define view boundaries

C code typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
// Eg
UIEdgeInsets insets = UIEdgeInsetsMake (5, 5, 5 );
CGRect innerRect = UIEdgeInsetsInsetRect ([aView bounds], insets );
CGRect subRect = CGRectInset (innerRect, self. frame. size. width/2.0f, self. frame. size. height/2.0f );

Affine transform supplement

// Create CGAffineTransform

C code // angle is better rotated between 0-2 * PI
CGAffineTransform transform = CGAffineTransformMakeRotation (angle );
// Zoom
CGAffineTransform transform = CGAffineTransformMakeScale (0.5f, 0.5f );
// Change the location
CGAffineTransform transform = CGAffineTransformMakeTranslation (50, 60 );
 
// Modify CGAffineTransform
// Modify Scaling
CGAffineTransform scaled = CGAffineTransformScale (transform, degree, degree );
// Modify the location
CGAffineTransform transform = CGAffineTransformTranslate (
CGAffineTransform t,
CGFloat tx,
CGFloat ty
);
 
// Modify the angle
CGAffineTransform transform = CGAffineTransformRotate (
CGAffineTransform t,
CGFloat angle
);
// Finally set to VIEW
[Self. view setTransform: scaled];
 
Create a UIView animation Block

// Create CGContextRef first

C code CGContextRef context = UIGraphicsGetCurrentContext ();
// Mark the start of the animation
[UIView beginAnimations: nil context: context];
// Define the animation acceleration or deceleration Mode
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
// Define the animation duration for 1 second
[UIView setAnimationDuration: 1.0];
// Intermediate processing of location change, size change, rotation, and so on
[[Self. view viewWithTag: 999] setAlpha: 1.0f];
// Indicates that the animation block ends.
[UIView commitAnimations];
// You can also set the callback.
[UIView setAnimationDelegate: self];
// Set the callback call Method
[UIView setAnimationDidStopSelector: @ selector (animationFinished :)];

View flip

 

C code UIView * whiteBackdrop = [self. view viewWithTag: 100];
// Choose left or right flip select left or right flip
If ([(UISegmentedControl *) self. navigationItem. titleView selectedSegmentIndex]) {
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView: whiteBackdrop cache: YES];
} Else {
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: whiteBackdrop cache: YES];
}
NSInteger purple = [[whiteBackdrop subviews] indexOfObject: [whiteBackdrop viewWithTag: 999];
NSInteger maroon = [[whiteBackdrop subviews] indexOfObject: [whiteBackdrop viewWithTag: 998];
// Exchange view
[WhiteBackdrop exchangeSubviewAtIndex: purple withSubviewAtIndex: maroon];
 
// There are two types of flip-up and flip-down:
Typedef enum {
// No Effect
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;

Use QuartzCore for animation

 

C code // create CATransition
CATransition * animation = [CATransition animation];
// Set proxy
Animation. delegate = self;
// Set the animation transition time
Animation. duration = 4.0f;
// Define the animation acceleration or deceleration Mode
Animation. timingFunction = UIViewAnimationCurveEaseInOut;
// Animation. type indicates the type of transition, such as Fade, MoveIn, Push, and Reveal.
Switch ([(UISegmentedControl *) self. navigationItem. titleView selectedSegmentIndex]) {
Case 0:
Animation. type = kCATransitionFade;
Break;
Case 1:
Animation. type = kCATransitionMoveIn;
Break;
Case 2:
Animation. type = kCATransitionPush;
Break;
Case 3:
Animation. type = kCATransitionReveal;
Default:
Break;
}
// Set the gradient direction.
If (isLeft)
Animation. subtype = kCATransitionFromRight;
Else
Animation. subtype = kCATransitionFromLeft;
 
// Perform the animation
UIView * whitebg = [self. view viewWithTag: 10];
NSInteger purple = [[whitebg subviews] indexOfObject: [whitebg viewWithTag: 99];
NSInteger white = [[whitebg subviews] indexOfObject: [whitebg viewWithTag: 100];
[Whitebg exchangeSubviewAtIndex: purple withSubviewAtIndex: white];
[[Whitebg layer] addAnimation: animation forKey: @ "animation"];
 
Animation. type can also be assigned with the following values:

 

C code switch (theButton. tag ){
Case 0:
Animation. type = @ "cube ";
Break;
Case 1:
Animation. type = @ "suckEffect ";
Break;
Case 2:
Animation. type = @ "oglFlip ";
Break;
Case 3:
Animation. type = @ "rippleEffect ";
Break;
Case 4:
Animation. type = @ "pageCurl ";
Break;
Case 5:
Animation. type = @ "pageUnCurl ";
Break;
Case 6:
Animation. type = @ "cameraIrisHollowOpen ";
Break;
Case 7:
Animation. type = @ "cameraIrisHollowClose ";
Break;
Default:
Break;
}

 

Sleep

 

C code [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0f];
 
A simple animation made from images

 

C code // Load butterfly images
NSMutableArray * bflies = [NSMutableArray array];
For (int I = 1; I <= 17; I ++ ){
[Bflies addObject: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: @ "bf _ % d", I] ofType: @ "png"];
}
UIImageView * butterflyView = [[UIImageView alloc] initWithFrame: CGRectMake (401_f, 3001_f, 601_f, 601_f)];
ButterflyView. tag= 300;
// Set the Animated Image
ButterflyView. animationImages = bflies;
// Set the time
ButterflyView. animationDuration = 0.75f;
[Self. view addSubview: butterflyView];
// Start the animation
[ButterflyView startAnimating];
[ButterflyView release];
 

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.