IOS Snowflake Effect

Source: Internet
Author: User
<span id="Label3"></p><span style="background-color: #00ccff;"><span style="background-color: #00ccff;">1.transform Property Introduction</span></span><p><p>In oc, you can modify the translation, scaling, and rotation angles of an object by using the Transform property</p></p><p><p>Common methods for creating transform structures are divided into two main categories</p></p><p><p>(1) create a deformation based on the initial position of the control</p></p><p><p>Cgaffinetransformmaketranslation (pan)</p></p><p><p>Cgaffinetransformmakescale (zoom)</p></p><p><p>Cgaffinetransformmakerotation (swivel)</p></p><p><p><span style="line-height: 1.5;">(2) Create a "transform parameter-based" deformation</span></p></p><p><p>Cgaffinetransformtranslate</p></p><p><p>Cgaffinetransformscale</p></p><p><p>Cgaffinetransformrotate</p></p><span style="background-color: #00ccff;"><span style="background-color: #00ccff;">2. Code Implementation</span></span><pre><span style="font-size: 14px;"><span style="color: #0000ff;"><span style="color: #0000ff;">#import</span></span> <span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">ViewController.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#define</span></span>Degree2angle (angle) ((angle) * m_pi/180)<br><span style="color: #0000ff;"><span style="color: #0000ff;">@interface</span></span><span style="color: #000000;"><span style="color: #000000;">viewcontroller () @property (nonatomic, Strong) cadisplaylink</span></span>*<span style="color: #000000;"><span style="color: #000000;">a timer; a @property (nonatomic, assign)</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Long</span></span><span style="color: #000000;"><span style="color: #000000;">steps;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@implementation</span></span><span style="color: #000000;"><span style="color: #000000;">Viewcontroller</span></span>-(cadisplaylink *<span style="color: #000000;"><span style="color: #000000;">) timer{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(_timer = =<span style="color: #000000;"><span style="color: #000000;">Nil) {_timer</span></span>=<span style="color: #000000;"><span style="color: #000000;">[cadisplaylink displaylinkwithtarget:self selector: @selector (update)]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">_timer;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Loadview{uiimageview</span></span>*BG =<span style="color: #000000;"><span style="color: #000000;">[[uiimageview Alloc]initwithframe:[uiscreen mainscreen].bounds]; Bg.image</span></span>= [UIImage Imagenamed:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">snowbg.jpg</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Bg.contentmode</span></span>=<span style="color: #000000;"><span style="color: #000000;">uiviewcontentmodescaleaspectfill; Bg.userinteractionenabled</span></span>=<span style="color: #000000;"><span style="color: #000000;">YES; Self.view</span></span>=<span style="color: #000000;"><span style="color: #000000;">bg;}<br></span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; [self.timer Addtorunloop:[nsrunloop mainrunloop] formode:nsdefaultrunloopmode];}<br></span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">the animation stops when clicked</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) touchesbegan: (nsset *) touches withevent: (uievent *)<span style="color: #0000ff;"><span style="color: #0000ff;">Event</span></span><span style="color: #000000;"><span style="color: #000000;">{[self stoptimer];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Stoptimer{[self.timer invalidate];}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Update{_steps</span></span>++<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Execute 5 times to add one time</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(_steps%<span style="color: #800080;"><span style="color: #800080;">5</span></span>==<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">) {[self snow]; }}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Hide the status bar</span></span>-<span style="color: #000000;"><span style="color: #000000;">(BOOL) prefersstatusbarhidden{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">YES;}</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">method One with the Transform property setting</span></span><span style="color: #008000;"><span style="color: #008000;">/*</span></span><span style="color: #008000;">-(void) snow{//1. Instantiate an image view of a snowflake UIImage *image = [UIImage imagenamed:@ "snowflake"]; Uiimageview *imageview = [[uiimageview alloc]initwithimage:image]; 0.1~0.4 randomly in 0.01~0.4 scaling cgfloat scale = Arc4random_uniform (40)/100.0 + 0.01; Set snowflake Scaling imageview.transform = Cgaffinetransformmakescale (scales, scale);<br>Cgsize winsize = self.view.bounds.size;<br>2. Add to view cgfloat x = Arc4random_uniform (winsize.width); CGFloat y =-imageview.bounds.size.height * 0.5; Imageview.center = Cgpointmake (x, y); [self.view addsubview:imageview]; 3. Animation drop [UIView animatewithduration:10.0f animations:^{//move to end position cgfloat ToX = Arc4random_uniform (win size.width); CGFloat ToY = winsize.height + imageView.bounds.size.height * 0.5; Imageview.center = Cgpointmake (toX, toY); Under process, set snowflake 360 degree random rotation imageview.transform = cgaffinetransformrotate (imageview.transform, degree2angle (arc4random_un Iform (360))); Imageview.alpha = 0.3; } completion:^ (BOOL finished) {[imageView removefromsuperview]; }];}</span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">method Two with KVC setting</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) snow{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1. Instantiate an image view of a snowflake</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dimensions of the entire view</span></span><span style="color: #000000;"><span style="color: #000000;">cgsize winsize</span></span>=<span style="color: #000000;"><span style="color: #000000;">self.view.bounds.size; UIImage</span></span>*image = [UIImage imagenamed:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Snowflakes</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">];<br> </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">2. Add to view</span></span><span style="color: #000000;"><span style="color: #000000;">Uiimageview</span></span>*imageview =<span style="color: #000000;"><span style="color: #000000;">[[uiimageview alloc]initwithimage:image]; CGFloat StartX</span></span>=<span style="color: #000000;"><span style="color: #000000;">Arc4random_uniform (winsize.width); CGFloat Starty</span></span>= -<span style="color: #000000;"><span style="color: #000000;">imageView.bounds.size.height; Imageview.center</span></span>=<span style="color: #000000;"><span style="color: #000000;">Cgpointmake (startX, starty); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">0.01~0.4 randomly in 0.01~0.4 zoom</span></span><span style="color: #000000;"><span style="color: #000000;">CGFloat</span> scale</span>= Arc4random_uniform (<span style="color: #800080;"><span style="color: #800080;"></span> +</span>)/<span style="color: #800080;"><span style="color: #800080;">100.0</span></span>+<span style="color: #800080;"><span style="color: #800080;">0.01</span></span><span style="color: #000000;"><span style="color: #000000;">; [imageview.layer setvalue:@ (scale) forkeypath:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Transform.scale</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [self.view addsubview:imageview]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">3. Animation whereabouts</span></span><span style="color: #000000;"><span style="color: #000000;">[UIView animatewithduration:</span></span><span style="color: #800080;"><span style="color: #800080;">10.0f</span></span>animations:^<span style="color: #000000;"><span style="color: #000000;">{ </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">move to end location</span></span><span style="color: #000000;"><span style="color: #000000;">cgfloat EndX</span></span>=<span style="color: #000000;"><span style="color: #000000;">Arc4random_uniform (winsize.width); CGFloat EndY</span></span>= Winsize.height +<span style="color: #000000;"><span style="color: #000000;">imageView.bounds.size.height; Imageview.center</span></span>=<span style="color: #000000;"><span style="color: #000000;">Cgpointmake (endx, endY); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">180 degrees of rotation during the fall</span></span><span style="color: #000000;"><span style="color: #000000;">[imageview.layer setvalue:@ (m_pi) forkeypath:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">transform.rotation</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">opacity <span style="color: #008000;">is 0.3</span></span><span style="color: #000000;"><span style="color: #000000;">[imageview.layer setvalue:@ (</span></span><span style="color: #800080;"><span style="color: #800080;">0.3</span></span>) Forkeypath:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Opacity</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; } completion:</span></span>^<span style="color: #000000;"><span style="color: #000000;">(BOOL Finished) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">End Animation removal ImageView</span></span><span style="color: #000000;"><span style="color: #000000;">[imageView removefromsuperview]; }]; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">@end</span></span></span></pre><p><p><span style="font-size: 14px;"></span></p></p><p><p>IOS Snowflake Effect</p></p></span>

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.