实现效果:点击该区域,上面的图片开始以position为中center,开始左右摇摆,摆动一个周期。
起初:我用UIButton来实现,认为button的当前图片和背景图片的变动。但是在查看button的属性后,我发现只能实现image和background的切换,而不能实现image的动态(动画)摇摆效果。后来:我通过搜索资料,发现要用到ImageView来实现。现在:还请各位大牛给点建议。不胜感激。要是能有实现的代码,那就更好了。先谢谢了。

解决方案 »

  1.   

    补充:可否使用ImageView 来操作重叠的图片。
    还是用到UITouch的方法
    如有知道的,请给予详细的解答,谢谢。
      

  2.   

    UIImageView *myAnimView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:myAnimView];
         CALayer *layer = [CALayer layer];
    [layer setBackgroundColor:[[UIColor greenColor] CGColor]];
    layer.bounds = CGRectMake(0, 0, 20.0, 30.0);
    layer.position = CGPointMake(myAnimView.center.x,myAnimView.center.y);
    [myAnimView.layer addSublayer:layer];

    //运行轨迹
    CGMutablePathRef thePath = CGPathCreateMutable();
        CGPathMoveToPoint(thePath, NULL, myAnimView.center.x, myAnimView.center.y);
        CGPathAddLineToPoint(thePath, NULL, myAnimView.center.x-5, myAnimView.center.y);
    CGPathAddLineToPoint(thePath, NULL, myAnimView.center.x, myAnimView.center.y);
    CGPathAddLineToPoint(thePath, NULL, myAnimView.center.x+5, myAnimView.center.y);
        CGPathAddLineToPoint(thePath, NULL, myAnimView.center.x, myAnimView.center.y);
    CGPathAddLineToPoint(thePath, NULL, myAnimView.center.x-5, myAnimView.center.y);
    CGPathAddLineToPoint(thePath, NULL, myAnimView.center.x, myAnimView.center.y);
        CGPathAddLineToPoint(thePath, NULL, myAnimView.center.x+5, myAnimView.center.y);
        
        
        CAKeyframeAnimation * animation;
        animation = [CAKeyframeAnimation animationWithKeyPath: @"position"];
        animation.path = thePath;
        animation.duration = 1.0;
        animation.repeatCount = 3; //重复周期
        CFRelease(thePath);
        [animation setDelegate:self];
        //[self animationDidStop:animation finished:YES];
        [animation setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear]];
        [layer addAnimation:animation forKey:kCATransition];
        
        [myAnimView release];
    实现时加库文件  <QuartzCore/QuartzCore.h>