zoukankan      html  css  js  c++  java
  • 用贝塞尔曲线实现水波效果

           // AppDelegate .h 文件

                    #import <UIKit/UIKit.h>

                   @interface AppDelegate : UIResponder <UIApplicationDelegate>

                   @property (strong, nonatomic) UIWindow *window;

                   @end

     

        // AppDelegate .m  文件

                #import "AppDelegate.h"

                #import "WavesView.h"

                @implementation AppDelegate 

                - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

                          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

                           // Override point for customization after application launch.

                         self.window.backgroundColor = [UIColor whiteColor];

                         WavesView *waves = [[WavesView alloc] init];

                         waves.frame = [[UIScreen mainScreen] bounds];

                         waves.backgroundColor = [UIColor whiteColor];

                         [self.window addSubview:waves];

                         [self.window makeKeyAndVisible];

                         return YES;

               }

              @end

           // WavesView .h 文件

               #import <UIKit/UIKit.h>

               @interface WavesView : UIView

               {

                        float a;

                    float b;

                    BOOL flag;

               }

               @end

     

     

           // WavesView .m 文件

           

             #import "WavesView.h"

             @implementation WavesView

             - (id)init

             {

                  if (self = [super init]) {

                  a = 5; 

                  b = 0;

                  flag = NO;

                 [NSTimer scheduledTimerWithTimeInterval:0.025 target:self selector:@selector(animateWave) userInfo:nil repeats:YES];

                  }

                  return self; 

             }

             - (void)animateWave

            {

                   if (flag) {

                        a += 0.1;

                   }else{

                        a -= 0.1;

                   }

                   if (a <= 4) {

                        flag = YES;

                   }

                   if (a >= 5) {

                        flag = NO;

                   }

                        b += 0.1;

                  [self setNeedsDisplay];

            }

           - (void)drawRect:(CGRect)rect

          {

                   UIBezierPath *path = [[UIBezierPath alloc] init];

                   //线条的起始点

                  [path moveToPoint:CGPointMake(0, 160)];

                  float y = 160;

                 for (float x = 0; x <= 320; x++) {

                        /*画正弦函数曲线从(0,160)到(320,160),

                       其中a的值来回变化导致曲线峰值来回变化,

                       b值不断增加导致曲线不断横行位移,

                       a,b两个值结合一起变化,从而形成波浪的效果

                      */

                         y = a*sin(x/180.0*M_PI +  b * 4 / M_PI)*5 + 160;

                        [path addLineToPoint:CGPointMake(x, y)];

            

                 }

                 /*下面几条线为了形成闭合的图形*/

                [path addLineToPoint:CGPointMake(320, self.frame.size.height)];

                [path addLineToPoint:CGPointMake(0, self.frame.size.height)];

                [path addLineToPoint:CGPointMake(0, 160)];

                 // 颜色

               UIColor *color = [UIColor colorWithRed:86/255.0 green:202/255.0 blue:139/255.0 alpha:1];

               //  填充颜色

               [color set];

               [path fill];

               path.lineWidth = 1.0f;

              [path stroke];

          } 

          @end

              // 效果图

               

  • 相关阅读:
    全新的ASP.NET !
    asp.net core+ef core
    直播服务器Nginx
    NET Core1
    网络爬虫1
    java thread reuse(good)
    java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)
    小心LinkedHashMap的get()方法(转)
    LinkedHashMap相关信息介绍(转)
    HashTable和HashMap的区别
  • 原文地址:https://www.cnblogs.com/lantu1989/p/4602686.html
Copyright © 2011-2022 走看看