第二版动画效果应用

 

实现简单的动画效果中编写了个动画效果示例。实际使用中发现问题。因为图片是占据整个屏幕的,在该示例中是768×1024,因此动画缩放的时候,不是针对剪辑图片部分的缩放,而是针对全图的缩放。

现在改一下,基本思路是,先要用矩形框获取图的一部分(参考截取部分图片并显示),生成一个新的图,然后再用不规则形状截取图片的办法截取部分不规则内容。

这里的一个问题是截取矩形框,应该是不规则点围成面积的最小矩形框,即这个矩形框刚刚好包含这个不规则图形,没有再小的矩形框能包含该图形。算法不难,需要取所有点的最左、最右、最上、最下四个坐标即可。

这回实现的样子:

imageimage

代码如下:

- (void)loadView { 
    //去掉最顶端的状态拦 
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide]; 
    
    //创建本例中唯一的一张原图 
    UIImage *image=[UIImage imageNamed:@"1.jpg"]; 
    
    //创建背景视图 
    self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    UIImageView *backgroudView=[[UIImageView alloc] initWithImage:image]; 
    backgroudView.alpha=0.3; 
    [self.view addSubview:backgroudView]; 
    
    //创建前景视图,从左上坐标系定位100,100开始,100,100长宽的视图 
    UIImageView *foregroundView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
    [self.view addSubview:foregroundView]; 
    //使用上面原图,左上坐标系坐标120,120开始,取一个100,100长宽的图 
    foregroundView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], 
                                                                                CGRectMake(120, 120, 100, 100))]; 
    [self.view addSubview:foregroundView]; 
    
    //在前景矩形图中画不规则图形 
    CGContextRef context = CGBitmapContextCreate(NULL, 100, 100, 8, 4 * 768, 
                                                 CGColorSpaceCreateDeviceRGB(), 
                                                 kCGImageAlphaPremultipliedFirst); 
    CGContextMoveToPoint(context, 10.0f, 10.0f); 
    CGContextAddLineToPoint(context, 10.0f, 100.0f); 
    CGContextAddLineToPoint(context, 90.0f, 90.0f); 
    CGContextAddLineToPoint(context, 90.0f, 10.0f); 
    CGContextAddLineToPoint(context, 44.0f, 1.0f); 
    
    CGContextClosePath(context); 
    CGContextClip(context); 
    CGContextDrawImage(context, CGRectMake(0, 0, 100, 100), foregroundView.image.CGImage); 
    CGImageRef imageMasked = CGBitmapContextCreateImage(context); 
    CGContextRelease(context); 
    UIImage *newImage = [UIImage imageWithCGImage:imageMasked]; 
    CGImageRelease(imageMasked); 
    foregroundView.image=newImage; 
    
    //设置动画 
    CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
    theAnimation.toValue = [NSNumber numberWithDouble:2.05]; 
    theAnimation.duration=0.5;//动画持续时间 
    theAnimation.repeatCount=10;//动画重复次数 
    theAnimation.autoreverses=YES;//是否自动重复 
    
    [foregroundView.layer addAnimation:theAnimation forKey:@"animateLayer"]; 
     
    [backgroudView release]; 
    [foregroundView release]; 
    [newImage release]; 
    [image release]; 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值