椭圆运动

本文介绍了一个iOS应用中视图沿椭圆路径运动的实现方法。通过定义椭圆方程,并利用NSTimer周期性更新视图的位置,使得按钮能够沿椭圆轨迹进行旋转运动。该方法适用于模拟天体运动等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  YGJViewController.m
//
#define         PX              160
#define         PY              240
#define         TUOYUAN_A       100
#define         TUOYUAN_B       100
#define         TUOYUAN_A1       50
#define         TUOYUAN_B1       50
#define         JIAODU_2_HUDU(x)   (x)/180.0*M_PI)
#define ZIZHUANJIAODU_2_HUDU    _angel1/180.0*M_PI
#import "YGJViewController.h"
#import "YGJTianQiViewController.h"
#import "YGJAppDelegate.h"
@interface YGJViewController ()
{
    int _angel;
    NSTimer *_timer;
    int _count;
}
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;

@end

@implementation YGJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)btn1Click:(id)sender {
    YGJTianQiViewController *vc=[[YGJTianQiViewController alloc]init];
    YGJAppDelegate *app=[UIApplication sharedApplication].delegate;
    UIWindow *window=app.window;
    window.rootViewController=vc;
    

    
    
_timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gongZhuan) userInfo:nil repeats:YES];
    
    
    
}
- (IBAction)btn2Click:(id)sender {
    
    _timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gongZhuan) userInfo:nil repeats:YES];
    
   
}
- (IBAction)btn3Click:(id)sender {
       _timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gongZhuan) userInfo:nil repeats:YES];
  

}
- (IBAction)btn4Click:(id)sender {
     _timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(gongZhuan) userInfo:nil repeats:YES];
    
   
}

- (IBAction)yuanZhouyunDong:(id)sender {
    
    }
-(void)gongZhuan
{
    /*

          椭圆的标准方程:x^2/a^2 + y^2/b^2 = 1

*/

_angel++; if (_angel%90==0) { [_timer invalidate]; } float x=PX + TUOYUAN_A*cos(JIAODU_2_HUDU(_angel-90); float y=PY + TUOYUAN_B*sin(JIAODU_2_HUDU(_angel-90); _btn1.center=CGPointMake(x, y); float x4=PX + TUOYUAN_A*cos(JIAODU_2_HUDU(_angel); float y4=PY + TUOYUAN_B*sin(JIAODU_2_HUDU(_angel); _btn4.center=CGPointMake(x4, y4); float x3=PX + TUOYUAN_A*cos(JIAODU_2_HUDU(_angel+90); float y3=PY + TUOYUAN_B*sin(JIAODU_2_HUDU(_angel+90); _btn3.center=CGPointMake(x3, y3); float x2=PX + TUOYUAN_A*cos(JIAODU_2_HUDU(_angel+180); float y2=PY + TUOYUAN_B*sin(JIAODU_2_HUDU(_angel+180); _btn2.center=CGPointMake(x2, y2); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值