浪费。 2019-04-24 16:51 采纳率: 50%
浏览 322
已采纳

请问这个进度球如何实现复用。

.h文件

#import <UIKit/UIKit.h>

@interface HWWaveView : UIView

@property (nonatomic, assign) CGFloat progress;

@end

.m文件

#import "HWWaveView.h"

#define KHWWaveFillColor [UIColor groupTableViewBackgroundColor] //填充颜色
#define KHWWaveTopColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:1.0f] //前面波浪颜色
#define KHWWaveBottomColor [UIColor colorWithRed:0/255.0 green:191/255.0 blue:255/255.0 alpha:0.4f] //后面波浪颜色

@interface HWWaveView ()

@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) CGFloat wave_amplitude;//振幅a(y = asin(wx+φ) + k)
@property (nonatomic, assign) CGFloat wave_cycle;//周期w
@property (nonatomic, assign) CGFloat wave_h_distance;//两个波水平之间偏移
@property (nonatomic, assign) CGFloat wave_v_distance;//两个波竖直之间偏移
@property (nonatomic, assign) CGFloat wave_scale;//水波速率
@property (nonatomic, assign) CGFloat wave_offsety;//波峰所在位置的y坐标
@property (nonatomic, assign) CGFloat wave_move_width;//移动的距离,配合速率设置
@property (nonatomic, assign) CGFloat wave_offsetx;//偏移
@property (nonatomic, assign) CGFloat offsety_scale;//上升的速度

@end

@implementation HWWaveView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];

        //初始化信息
        [self initInfo];
    }

    return self;
}

- (void)initInfo
{
    //进度
    _progress = 0;
    //振幅
    _wave_amplitude = self.frame.size.height / 25;
    //周期
    _wave_cycle = 2 * M_PI / (self.frame.size.width * 0.9);
    //两个波水平之间偏移
    _wave_h_distance = 2 * M_PI / _wave_cycle * 0.6;
    //两个波竖直之间偏移
    _wave_v_distance = _wave_amplitude * 0.4;
    //移动的距离,配合速率设置
    _wave_move_width = 0.5;
    //水波速率
    _wave_scale = 0.4;
    //上升的速度
    _offsety_scale = 0.1;
    //波峰所在位置的y坐标,刚开始的时候_wave_offsety是最大值
    _wave_offsety = (1 - _progress) * (self.frame.size.height + 2 * _wave_amplitude);

    [self addDisplayLinkAction];
}

- (void)addDisplayLinkAction
{
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction)];
    [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)displayLinkAction
{
    _wave_offsetx += _wave_move_width * _wave_scale;

    //完成
    if (_wave_offsety <= 0.01)  [self removeDisplayLinkAction];

    [self setNeedsDisplay];
}

- (void)removeDisplayLinkAction
{
    [_displayLink invalidate];
    _displayLink = nil;
}

- (void)drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
    [KHWWaveFillColor setFill];
    [path fill];
    [path addClip];

    //绘制两个波形图
    [self drawWaveColor:KHWWaveTopColor offsetx:0 offsety:0];
    [self drawWaveColor:KHWWaveBottomColor offsetx:_wave_h_distance offsety:_wave_v_distance];
}

- (void)drawWaveColor:(UIColor *)color offsetx:(CGFloat)offsetx offsety:(CGFloat)offsety
{
    //波浪动画,进度的实际操作范围是,多加上两个振幅的高度,到达设置进度的位置y
    CGFloat end_offY = (1 - _progress) * (self.frame.size.height + 2 * _wave_amplitude);
    if (_wave_offsety != end_offY) {
        if (end_offY < _wave_offsety) {
            _wave_offsety = MAX(_wave_offsety -= (_wave_offsety - end_offY) * _offsety_scale, end_offY);
        }else {
            _wave_offsety = MIN(_wave_offsety += (end_offY - _wave_offsety) * _offsety_scale, end_offY);
        }
    }

    UIBezierPath *wavePath = [UIBezierPath bezierPath];
    for (float next_x = 0.f; next_x <= self.frame.size.width; next_x ++) {
        //正弦函数,绘制波形
        CGFloat next_y = _wave_amplitude * sin(_wave_cycle * next_x + _wave_offsetx + offsetx / self.bounds.size.width * 2 * M_PI) + _wave_offsety + offsety;
        if (next_x == 0) {
            [wavePath moveToPoint:CGPointMake(next_x, next_y - _wave_amplitude)];
        }else {
            [wavePath addLineToPoint:CGPointMake(next_x, next_y - _wave_amplitude)];
        }
    }

    [wavePath addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
    [wavePath addLineToPoint:CGPointMake(0, self.bounds.size.height)];
    [color set];
    [wavePath fill];

}

@end

控制器文件

#import "ViewController.h"
#import "HWWaveView.h"

#import "HWProgressView.h"


@interface ViewController ()

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, weak) HWWaveView *waveView;

@property (nonatomic, weak) HWProgressView *progressView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建控件
    [self creatControl];

    //添加定时器
    [self addTimer];
}

- (void)creatControl
{
    //波浪
    HWWaveView *waveView = [[HWWaveView alloc] initWithFrame:CGRectMake(30, 100, 150, 150)];
    [self.view addSubview:waveView];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    singleTap.numberOfTapsRequired = 1;
    [waveView addGestureRecognizer:singleTap];

    self.waveView = waveView;

    //进度条
    HWProgressView *progressView = [[HWProgressView alloc] initWithFrame:CGRectMake(30, 365, 150, 20)];
    [self.view addSubview:progressView];

    self.progressView = progressView;

}

- (void)addTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

- (void)timerAction
{
    //_waveView.progress += 0.01;

    _progressView.progress += 0.01;


    if (_progressView.progress >= 1) {
        [self removeTimer];
        NSLog(@"完成");
    }
}

- (void)removeTimer
{
    [_timer invalidate];
    _timer = nil;
}

-(void)singleTap:(UITapGestureRecognizer *)tap{
    _waveView.progress += 0.1;
    if (_waveView.progress == 0.9) {
        _waveView.progress = -0.1;
    }
}

@end

我尝试了在手势中重置progress的值,但是不起作用。所以想问一下如何实现复用这个球。就是当它被填满时再点一下又重新回到起始状态可以继续点

  • 写回答

1条回答 默认 最新

  • 潇潇沐雨洒江河 2019-04-25 15:29
    关注
    • (void)displayLinkAction
      {
      _wave_offsetx += _wave_move_width * _wave_scale;

      //完成
      // if (_wave_offsety <= 0.01) [self removeDisplayLinkAction];

      [self setNeedsDisplay];
      }

    • (void)drawWaveColor:(UIColor *)color offsetx:(CGFloat)offsetx offsety:(CGFloat)offsety
      {
      if (_progress >= 0.9) {
      [self.layer removeAllAnimations];
      _progress = 0.1;
      }

      -(void)singleTap:(UITapGestureRecognizer *)tap{
      

      _waveView.progress += 0.1;

    }
    保重

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常