二:Cursor的Mac端使用教程

前言:

上一篇文章已经介绍了,如何安装,此篇介绍使用,其实超级简单。

使用

首先用Cursor打开你的项目工程,我这里是用xcode建立的iOS的工程,作为使用示例。

1,打开右侧工具栏,在工具栏里面搜你需要做的描述。

image.png

打开1,会出现2,在2里面描述你的需求,描述完成后点击send

2,比如我得描述为如下:

我的目标:
    使用object-c写一个自定义view
要求:
     1.页面中包含一个抽奖按钮
     2.页面中包含一个转盘
     3.限制每个用户每天抽奖一次
     4.抽奖结果需要显示在页面上

描述完成后点击send
Cursor会在右侧生成代码和介绍,以及如何使用。
Cursor也会在你的工程里面自动创建文件和生成需求代码

3,它自己生成文件,并且写好代码,还教你如何使用
Cursor自动生成了两部分文件,分别是【控制器】和自定义【view】,连解耦封装都做了:
我得要求截图,Cursor创建的文件截图。

image.png

Cursor生成的文件具体内容如下:
controller.h里面

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LXLuckyWheelViewController : UIViewController

@end

NS_ASSUME_NONNULL_END 

controller.m里面

#import "LXLuckyWheelViewController.h"
#import "LXLuckyWheelView.h"

@interface LXLuckyWheelViewController () <LXLuckyWheelViewDelegate>

@property (nonatomic, strong) LXLuckyWheelView *wheelView;
@property (nonatomic, strong) NSString *lastPlayDate;

@end

@implementation LXLuckyWheelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 设置奖品列表
    NSArray *prizes = @[@"iPhone 15", @"100元红包", @"谢谢参与", @"50元红包", 
                       @"AirPods", @"10元红包", @"优惠券", @"5元红包"];
    
    // 创建转盘视图
    self.wheelView = [[LXLuckyWheelView alloc] initWithFrame:CGRectMake(0, 0, 300, 400) 
                                                     prizes:prizes];
    self.wheelView.center = self.view.center;
    self.wheelView.delegate = self;
    [self.view addSubview:self.wheelView];
    
    // 从UserDefaults加载上次抽奖日期
    self.lastPlayDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"LastLuckyWheelPlayDate"];
}

#pragma mark - LXLuckyWheelViewDelegate
- (void)luckyWheelDidFinishWithPrize:(NSString *)prize {
    // 保存抽奖日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    self.lastPlayDate = [formatter stringFromDate:[NSDate date]];
    [[NSUserDefaults standardUserDefaults] setObject:self.lastPlayDate 
                                            forKey:@"LastLuckyWheelPlayDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    // 这里可以处理中奖逻辑,比如发放奖品等
    NSLog(@"用户获得奖品:%@", prize);
}

- (BOOL)canUserPlayToday {
    if (!self.lastPlayDate) {
        return YES;
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *today = [formatter stringFromDate:[NSDate date]];
    
    return ![today isEqualToString:self.lastPlayDate];
}

@end 

view.h里面

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@protocol LXLuckyWheelViewDelegate <NSObject>

- (void)luckyWheelDidFinishWithPrize:(NSString *)prize;
- (BOOL)canUserPlayToday;

@end

@interface LXLuckyWheelView : UIView

@property (nonatomic, weak) id<LXLuckyWheelViewDelegate> delegate;
@property (nonatomic, strong) NSArray *prizes; // 奖品列表
@property (nonatomic, assign, readonly) BOOL isSpinning; // 是否正在旋转

- (instancetype)initWithFrame:(CGRect)frame prizes:(NSArray *)prizes;
- (void)startSpinning;
- (void)showResult:(NSString *)prize;

@end

NS_ASSUME_NONNULL_END 

view.m里面

#import "LXLuckyWheelView.h"

@interface LXLuckyWheelView()

@property (nonatomic, strong) UIImageView *wheelImageView; // 转盘背景
@property (nonatomic, strong) UIButton *startButton; // 开始按钮
@property (nonatomic, strong) UILabel *resultLabel; // 结果显示
@property (nonatomic, assign, readwrite) BOOL isSpinning;
@property (nonatomic, strong) CADisplayLink *displayLink;
@property (nonatomic, assign) CGFloat currentRotation;
@property (nonatomic, assign) CGFloat targetRotation;
@property (nonatomic, assign) CGFloat rotationSpeed;

@end

@implementation LXLuckyWheelView

- (instancetype)initWithFrame:(CGRect)frame prizes:(NSArray *)prizes {
    self = [super initWithFrame:frame];
    if (self) {
        _prizes = prizes;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    // 设置转盘背景
    self.wheelImageView = [[UIImageView alloc] init];
    self.wheelImageView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.width);
    self.wheelImageView.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    self.wheelImageView.image = [UIImage imageNamed:@"lucky_wheel_bg"];
    [self addSubview:self.wheelImageView];
    
    // 设置开始按钮
    self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.startButton.frame = CGRectMake(0, 0, 80, 80);
    self.startButton.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    [self.startButton setTitle:@"开始" forState:UIControlStateNormal];
    [self.startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.startButton.backgroundColor = [UIColor redColor];
    self.startButton.layer.cornerRadius = 40;
    [self.startButton addTarget:self action:@selector(startButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.startButton];
    
    // 设置结果标签
    self.resultLabel = [[UILabel alloc] init];
    self.resultLabel.frame = CGRectMake(0, CGRectGetMaxY(self.wheelImageView.frame) + 20, self.bounds.size.width, 30);
    self.resultLabel.textAlignment = NSTextAlignmentCenter;
    self.resultLabel.textColor = [UIColor blackColor];
    self.resultLabel.hidden = YES;
    [self addSubview:self.resultLabel];
    
    // 绘制转盘分区
    [self drawWheelSections];
}

- (void)drawWheelSections {
    CGFloat anglePerSection = 2 * M_PI / self.prizes.count;
    CGPoint center = CGPointMake(self.wheelImageView.bounds.size.width/2, self.wheelImageView.bounds.size.height/2);
    CGFloat radius = self.wheelImageView.bounds.size.width/2 - 20;
    
    for (int i = 0; i < self.prizes.count; i++) {
        CGFloat startAngle = i * anglePerSection;
        CGFloat endAngle = (i + 1) * anglePerSection;
        
        // 创建扇形路径
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path moveToPoint:center];
        [path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
        [path closePath];
        
        // 设置颜色
        UIColor *color = (i % 2 == 0) ? [UIColor colorWithRed:1.0 green:0.8 blue:0.8 alpha:1.0] : [UIColor colorWithRed:0.8 green:1.0 blue:0.8 alpha:1.0];
        [color setFill];
        [path fill];
        
        // 添加奖品文字
        CGFloat textAngle = startAngle + anglePerSection/2;
        CGFloat textRadius = radius * 0.7;
        CGPoint textPoint = CGPointMake(
            center.x + textRadius * cos(textAngle),
            center.y + textRadius * sin(textAngle)
        );
        
        NSString *prize = self.prizes[I];
        UILabel *prizeLabel = [[UILabel alloc] init];
        prizeLabel.text = prize;
        prizeLabel.font = [UIFont systemFontOfSize:14];
        prizeLabel.textAlignment = NSTextAlignmentCenter;
        [prizeLabel sizeToFit];
        prizeLabel.center = textPoint;
        prizeLabel.transform = CGAffineTransformMakeRotation(textAngle + M_PI/2);
        [self.wheelImageView addSubview:prizeLabel];
    }
}

- (void)startButtonTapped {
    if (self.isSpinning) {
        return;
    }
    
    if (![self.delegate canUserPlayToday]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" 
                                                                     message:@"您今天已经抽过奖了,明天再来吧!" 
                                                              preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" 
                                                          style:UIAlertActionStyleDefault 
                                                        handler:nil];
        [alert addAction:okAction];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        return;
    }
    
    [self startSpinning];
}

- (void)startSpinning {
    self.isSpinning = YES;
    self.startButton.enabled = NO;
    
    // 随机选择一个奖品
    NSInteger randomIndex = arc4random_uniform((uint32_t)self.prizes.count);
    NSString *selectedPrize = self.prizes[randomIndex];
    
    // 计算目标角度
    CGFloat anglePerSection = 2 * M_PI / self.prizes.count;
    self.targetRotation = anglePerSection * randomIndex + 10 * M_PI; // 多转几圈
    self.currentRotation = 0;
    self.rotationSpeed = 0.5;
    
    // 开始动画
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateRotation)];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
    // 3秒后停止
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self finishSpinningWithPrize:selectedPrize];
    });
}

- (void)updateRotation {
    self.currentRotation += self.rotationSpeed;
    self.wheelImageView.transform = CGAffineTransformMakeRotation(self.currentRotation);
    
    // 减速效果
    if (self.currentRotation >= self.targetRotation * 0.7) {
        self.rotationSpeed *= 0.95;
    }
}

- (void)finishSpinningWithPrize:(NSString *)prize {
    [self.displayLink invalidate];
    self.displayLink = nil;
    self.isSpinning = NO;
    self.startButton.enabled = YES;
    
    [self showResult:prize];
    
    if ([self.delegate respondsToSelector:@selector(luckyWheelDidFinishWithPrize:)]) {
        [self.delegate luckyWheelDidFinishWithPrize:prize];
    }
}

- (void)showResult:(NSString *)prize {
    self.resultLabel.hidden = NO;
    self.resultLabel.text = [NSString stringWithFormat:@"恭喜您获得:%@", prize];
    
    // 添加一个简单的动画效果
    self.resultLabel.transform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration:0.3 animations:^{
        self.resultLabel.transform = CGAffineTransformIdentity;
    }];
}

@end 

教你如何使用:

LXLuckyWheelViewController *wheelVC = [[LXLuckyWheelViewController alloc] init];
[self.navigationController pushViewController:wheelVC animated:YES];

到此结束,你可以在拿到此文件去打开xcode调试就行了。
注:Cursor安装iOS的插件,应该也可以运行看效果。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容