视图动画沿轨迹运动


导入图片如

视图动画沿轨迹运动

1、ViewController.m文件里

- (void)viewDidLoad
{
      [super viewDidLoad];
      TouchView *tv=[[TouchView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
      tv.backgroundColor=[UIColor redColor];
      [self.view addSubview:tv];
}
2、创建UIView子类TouchView
TouchView.h文件

#import

@interface TouchView : UIView
@property(retain,nonatomic)NSMutableArray *lineArray,*pointArray;
@property(retain,nonatomic)UIImageView *imagv;
@property(retain,nonatomic)NSTimer *tm;
@property(assign,nonatomic)int counter;
@end

TouchView.m文件
#import "TouchView.h"

@implementation TouchView
@synthesize lineArray,pointArray,imagv,tm,counter;
- (id)initWithFrame:(CGRect)frame
{
      self = [super initWithFrame:frame];
      if (self) {
             
             
              self.userInteractionEnabled=YES;
             
              self.lineArray=[[NSMutableArray alloc]init];//线数组创建初始化。写在这里是因为只创建一次就可以了
             
              //添加动画
            self.imagv=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 90, 90)];
              [self addSubview:imagv];
              NSMutableArray *pic=[[NSMutableArray alloc]init];
              for (int i=1; i<7; i++)
              {
                      NSString *picname=[NSString stringWithFormat:@"run%d.tiff",i];
                      UIImage *p=[UIImage imageNamed:picname];
                      [pic addObject:p];
              }
              self.imagv.animationImages=pic;
              self.imagv.animationDuration=3;
              [self.imagv startAnimating];
      }
      return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
      UITouch *uc=[touches anyObject];//从set中取出一个值
      CGPoint pt=[uc locationInView:self];//获取到触摸位置
      NSValue *pp=[NSValue valueWithCGPoint:pt];//将触摸位置CGPoint转化为NSValue类型
      self.pointArray=[[NSMutableArray alloc]init];//创建点数组
      [self.pointArray addObject:pp];//将起点存入数组
      [self.lineArray addObject:self.pointArray];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
      UITouch *uc=[touches anyObject];//从set中取出一个值
      CGPoint pt=[uc locationInView:self];//获取到触摸位置 
      NSValue *pp=[NSValue valueWithCGPoint:pt];//将触摸位置CGPoint转化为NSValue类型
      self.pointArray=[[NSMutableArray alloc]init];
      self.pointArray=[self.lineArray lastObject];//代表最后一笔
      [self.pointArray addObject:pp];//将移动中的经过的点存入数组
      [self setNeedsDisplay];//重绘界面 最后调用drawRect
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
      UITouch *uc=[touches anyObject];
      CGPoint pt=[uc locationInView:self];
      NSValue *pp=[NSValue valueWithCGPoint:pt];
      self.pointArray=[[NSMutableArray alloc]init];
      self.pointArray=[self.lineArray lastObject];
      [self.pointArray addObject:pp];
     
      self.tm=[NSTimer scheduledTimerWithTimeIn terval:0.2 target:self selector:@selector(move:) userInfo:nil repeats:YES];//创建一个定时器,每过0.2秒调用自己的move:方法
     
          self.counter=0; //设置count的值,使每次调用move之前 是从第一个点开始的
     
}
-(void)move:(id)sender
{
      if (self.counter<[self.pointArray count])
      {
              NSValue *aa=[self.pointArray objectAtIndex:self.counter++];
              CGPoint pp=[aa CGPointValue];
              self.imagv.center=pp;
              [self addSubview:self.imagv];
      }
     
      else
      {
                [self.tm invalidate];//保证到达最后一点后继续调用move 使时间函数废止
      }
   
   
}

- (void)drawRect:(CGRect)rect
{
      CGContextRef ref=UIGraphicsGetCurrentCont ext();//获取上下文
      CGContextSetLineWidth(ref, 5);//设置画笔线宽5
      CGColorRef cf=[[UIColor blueColor]CGColor];
      CGContextSetStrokeColorW ithColor(ref, cf);//将画笔设置为红色
      if([pointArray count]-1]>0)
      {
            for (int i=0; i<[pointArray count]-1; i++)//-1防止越界
      {
              NSValue *vv=[pointArray objectAtIndex:i];
              CGPoint pp=[vv CGPointValue];//起点
              NSValue *vv2=[pointArray objectAtIndex:i+1];
              CGPoint pp2=[vv2 CGPointValue];//终点
              CGContextMoveToPoint(ref, pp.x, pp.y);
              CGContextAddLineToPoint(ref, pp2.x, pp2.y);
      }
      CGContextStrokePath(ref);//具体执行画图动作

}
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值