iOS 简单的操作杆旋转实现示例详解

 更新时间:2022年12月29日 12:17:03   作者:头疼脑胀的代码搬运工  
这篇文章主要为大家介绍了iOS 简单的操作杆旋转实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

一、效果实现

简单实现了一个消灭病毒的小效果,画面略显粗糙,多多见谅

控制球复位

二、操作杆实现

实现拖动小球,获取当前小球的旋转方向,将旋转的方向传递出去,旋转“坦克”进行攻击“病毒”。

#import "DirectionOptionView.h"
@interface DirectionOptionView()
//方向指示器滚珠
@property(nonatomic,strong) UIView * ball;
@end
@implementation DirectionOptionView
- (instancetype)initWithFrame:(CGRect)frame changeDirectionBlock:(ChangeDirectionBlock)changeDirectionBlock
{
    if (self = [super initWithFrame:frame]) {
        self.changeDirectionBlock = changeDirectionBlock;
        [self makeView];
        [self addPan];
    }
    return self;
}
//添加拖拽手势
- (void)addPan{
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector(panAction:)];
    [self addGestureRecognizer:pan];
}
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
        {
            CGPoint point = [pan locationInView:**self**];
            self.ball.alpha = 1;
            [self moveWithPoint:point];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            CGPoint point = [pan locationInView:self];
            [self moveWithPoint:point];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            CGPoint point = [pan locationInView:self];
            [self resetBallPositionWithEndPoint:point];
        }
            break;
        default:
            break;
    }
}
//小球复位
- (void)resetBallPositionWithEndPoint:(CGPoint)point
{
    self.changeDirectionBlock(0);
    [UIView animateWithDuration:0.2 animations:^{
        self.ball.center = CGPointMake((self.frame.size.width / 2.0), (self.frame.size.height / 2.0));
        self.ball.alpha = 0.4;
    }];
}
//根据控制球位置获取当前旋转角度
- (void)moveWithPoint:(CGPoint)point
{
    CGFloat distanceCircle = (self.ball.frame.size.width / 2.0);
    CGFloat x = point.x;
    CGFloat y = point.y;
    CGFloat dx = x - self.frame.size.width / 2.0;
    CGFloat dy = y - self.frame.size.height / 2.0;
    CGFloat rotation = atan2(dx,dy);
    CGFloat r = self.frame.size.width / 2.0;
    CGFloat rx = (r - distanceCircle) * sin(rotation) + r;
    CGFloat ry = (r - distanceCircle) * cos(rotation) + r;
    //防止控制球越界
    if ((sqrt((dx * dx) + (dy * dy))) > (r - distanceCircle)) {
        x = rx;
        y = ry;
    }
    //用block形式向外界暴露当前控制球相对于屏幕上方的角度
    self.changeDirectionBlock(-rotation + M_PI);
    self.ball.center = CGPointMake(x, y);
}
- (void)makeView{
    //进行倒角
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = self.frame.size.width / 2.0;
    self.backgroundColor = [[UIColor groupTableViewBackgroundColor] colorWithAlphaComponent:0.7];
    //添加控制球
    [self addSubview:self.ball];
}
//控制球
- (UIView *)ball
{
    if (!_ball) {
        CGSize size = CGSizeMake(45, 45);
        _ball = [[UIView alloc] initWithFrame:CGRectMake((self.frame.size.width - size.width) / 2.0, (self.frame.size.height - size.height) / 2.0, size.width, size.height)];
        _ball.alpha = 0.4;
        _ball.layer.masksToBounds = YES;
        _ball.layer.cornerRadius = _ball.frame.size.width / 2.0;
        _ball.backgroundColor = [UIColor lightGrayColor];
    }
    return _ball;
}
@end

三、发射子弹及碰撞检测

1、发射子弹

//根据当前角度,预判子弹动画的结束位置
- (NSArray *)prepareBulletPath
{
    CGPoint center = self.center;
    CGFloat maxLength = sqrt(([UIScreen mainScreen].bounds.size.width * [UIScreen mainScreen].bounds.size.width) + ([UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].bounds.size.height));
    CGFloat endY = sin(self.angle - M_PI / 2.0) * maxLength + center.y;
    CGFloat endX = cos(self.angle - M_PI / 2.0) * maxLength + center.x;
    CGPoint endPoint = CGPointMake(endX, endY);
    return @[@(center),@(endPoint)];
}
- (void)fir
{
    CGFloat bulletWidth = 10;
    BulletView * lastBulletView = self.bulletArr.count > 0 ? self.bulletArr.lastObject : nil;
    if (!lastBulletView) {
        BulletView * view = [[BulletView alloc] initWithFrame:CGRectMake((self.frame.size.width - bulletWidth) / 2.0, 0,bulletWidth,bulletWidth)];
        view.points = [self prepareBulletPath];
        [self.superview insertSubview:view belowSubview:self];
        [self.bulletArr addObject:view];
        view.center = [view.points[0] CGPointValue];
        [UIView animateWithDuration:1 animations:^{
            view.center = [view.points[1] CGPointValue];
        } completion:^(BOOL finished) {
            [view removeFromSuperview];
            [self.bulletArr removeObject:view];
        }];
    }
}

fir 本身是一个定时器事件,在里面添加一些创建子弹的逻辑,位置移动还是用了最简单的 UIViewanimateWithDuration 方法,但是注意这里面通过 frame 进行碰撞检测是获取不到,所以,添加了 CADisplayLink 屏幕刷新事件来检测子弹视图的 layer.presentationLayer.frame来进行与病毒的 frame 进行检测屏幕位置是否包含。

2、检测碰撞

bool CGRectIntersectsRect(CGRect rect1, CGRect rect2) 检测碰撞的方法

//添加屏幕刷新事件监听
- (void)addScreenRefreshAction
{
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector: @selector(update)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)update
{
    [self.bulletArr enumerateObjectsUsingBlock:^(BulletView * bulletView, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.virusArr enumerateObjectsUsingBlock:^(VirusView * virusView, NSUInteger idx, BOOL * _Nonnull stop) {
            //检测碰撞
            if (CGRectIntersectsRect(bulletView.layer.presentationLayer.frame, virusView.frame)) {
                [bulletView removeFromSuperview];
                [self.bulletArr removeObject:bulletView];
                [virusView attacked];
                if ([virusView isDie]) {
                    [virusView removeFromSuperview];
                    [self.virusArr removeObject:virusView];
                }
            }
        }];
    }];
    if (!self.virusArr.count) {
        [self createVirusView];
    }
}

四、添加病毒及消灭动画

1、随机创建病毒

- (void)createVirusView
{
    int width = arc4random() % 30 + 50;
    int x = arc4random() % ([[NSNumber numberWithFloat:[UIScreen mainScreen].bounds.size.width] integerValue] - width);
    int y = arc4random() % ([[NSNumber numberWithFloat:[UIScreen mainScreen].bounds.size.height / 2.0] integerValue]);
    VirusView * virusView = [[VirusView alloc] initWithFrame:CGRectMake(x, y, width, width)];
    [self.superview addSubview:virusView];
    [self.virusArr addObject:virusView];
}

2、消灭动画

添加了一点粒子效果,显示病毒消散动画

- (void)fireExplode
{
    CAEmitterLayer * emitter = [CAEmitterLayer layer];
    emitter.frame = self.frame;
    [self.superview.layer addSublayer:emitter];
    emitter.renderMode = kCAEmitterLayerAdditive;
    emitter.emitterPosition = CGPointMake(emitter.frame.size.width*0.5,    emitter.frame.size.height*0.5);
    CAEmitterCell *cell = [[CAEmitterCell alloc] init];
    cell.contents = ( __bridge id)[UIImage imageNamed:@"v4"].CGImage;
    cell.birthRate = 1;//出生率
    cell.lifetime = 0.7;//生命周期
    cell.emissionLongitude = - M_PI_2;
    cell.emissionRange = M_PI_2;
    cell.alphaSpeed = -0.2;
    cell.velocity = 10;//速度
    cell.scale = 0.15;//缩放倍数
    emitter.emitterCells = @[cell];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [emitter removeFromSuperlayer];
    });
}

五、思考与总结

添加 UIViewanimateWithDuration 方法后,这里用的是屏幕刷新检测,来获取当前控件的 layer.presentationLayer.frame 来检测碰撞,其他逻辑都相对简单。

以上就是iOS 简单的操作杆旋转实现示例详解的详细内容,更多关于iOS 操作杆旋转的资料请关注脚本之家其它相关文章!

相关文章

  • ios多种语言的本地化思路

    ios多种语言的本地化思路

    ios程序实现多种语言的本地化办法,最近要对一款游戏进行多语言本地化,在网上找了一些方案,加上自己的一点点想法整理出一套方案和大家分享!
    2015-05-05
  • IOS 播放系统提示音使用总结(AudioToolbox)

    IOS 播放系统提示音使用总结(AudioToolbox)

    这篇文章主要介绍了IOS 播放系统提示音使用总结(AudioToolbox)的相关资料,需要的朋友可以参考下
    2017-05-05
  • iOS开发之触摸事件

    iOS开发之触摸事件

    iOS设备都是可以多点触摸的,是指手指放在iOS设备的屏幕上从屏幕上拖动或抬起。系统当前视图响应触摸事件,若无响应则向上层传递,构成响应者链。触摸事件的函数有4个。
    2016-04-04
  • iOS之UIWebView无法获取web标题的解决方法

    iOS之UIWebView无法获取web标题的解决方法

    这篇文章主要为大家详细介绍了iOS之UIWebView无法获取web标题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • 详解iOS的冲顶大会辅助

    详解iOS的冲顶大会辅助

    本篇文章主要介绍了详解iOS的冲顶大会辅助,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • iOS10 推送最新特性研究

    iOS10 推送最新特性研究

    这篇文章主要为大家详细研究了iOS10 推送的最新特性,推送内容更加丰富,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • IOS实现图片轮播无限循环效果

    IOS实现图片轮播无限循环效果

    这篇文章主要为大家详细介绍了IOS实现图片轮播无限循环效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • iOS小组件开发之WidgetKit功能讲解

    iOS小组件开发之WidgetKit功能讲解

    这篇文章主要为大家介绍了iOS小组件开发WidgetKit功能讲解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • iOS DispatchSourceTimer 定时器的具体使用

    iOS DispatchSourceTimer 定时器的具体使用

    定时器在很多地方都可以用到,本文主要介绍了iOS DispatchSourceTimer 定时器的具体使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • iOS仿抖音视频加载动画效果的实现方法

    iOS仿抖音视频加载动画效果的实现方法

    这篇文章主要给大家介绍了关于iOS视频加载动画效果的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,下面随着小编来一起学习学习吧
    2018-11-11

最新评论