开门动画

使用方式:

DoorView *doorView = [[DoorView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)
                                                door:[UIImage imageNamed:@"Default.png"]];
[doorView setDelegate:self];
[doorView openDoor];
[self.view addSubview:doorView];
[doorView release];

- (void)doorView:(DoorView *)doorView didDoorOpend:(BOOL)opend
{
    if(opend)
    {
        NSLog(@"Door Opend\n");
    }
}
.h文件
#import <UIKit/UIKit.h>

@protocol DoorViewDelegate;
@interface DoorView : UIView
{
    id<DoorViewDelegate> _delegate;
}
@property (nonatomic, assign) id<DoorViewDelegate> delegate;

- (id)initWithFrame:(CGRect)frame door:(UIImage *)doorImage;

- (void)openDoor;

@end

@protocol DoorViewDelegate <NSObject>

- (void)doorView:(DoorView *)doorView didDoorOpend:(BOOL)opend;

@end
.m文件

#import "DoorView.h"
#import <QuartzCore/QuartzCore.h>

@implementation DoorView

#define LEFT_DOOR_VIEW_TAG  (11)
#define RIGHT_DOOR_VIEW_TAG (12)

- (id)initWithFrame:(CGRect)frame door:(UIImage *)doorImage;
{
    self = [super initWithFrame:frame];
    if (self)
    {
        CGImageRef imageRef = doorImage.CGImage;
        
        CGSize  size  = doorImage.size;
        CGFloat scale = [UIScreen mainScreen].scale;
        
        // Left door
        UIImageView *leftDoorView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, -20.0, self.bounds.size.width / 2.0, self.bounds.size.height)];
        CGImageRef leftDoorImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(0.0, 0.0, size.width * scale / 2.0, size.height * scale));
        [leftDoorView setImage:[UIImage imageWithCGImage:leftDoorImageRef]];
        CGImageRelease(leftDoorImageRef);
        [leftDoorView setCenter:CGPointMake(leftDoorView.center.x - leftDoorView.bounds.size.width / 2.0f, leftDoorView.center.y)];
        [leftDoorView.layer setAnchorPoint:CGPointMake(0.0, 0.5)];
        [leftDoorView setTag:LEFT_DOOR_VIEW_TAG];
        [self addSubview:leftDoorView];
        [leftDoorView release];
        
        // Right door
        UIImageView *rightDoorView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width / 2.0, -20.0, self.bounds.size.width / 2.0, self.bounds.size.height)];
        CGImageRef rightDoorImageRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(size.width * scale / 2.0, 0.0, size.width * scale, size.height * scale));
        [rightDoorView setImage:[UIImage imageWithCGImage:rightDoorImageRef]];
        CGImageRelease(rightDoorImageRef);
        [rightDoorView setCenter:CGPointMake(rightDoorView.center.x + rightDoorView.bounds.size.width/2.0f, rightDoorView.center.y)];    [rightDoorView.layer setAnchorPoint:CGPointMake(1.0, 0.5)];
        [rightDoorView setTag:RIGHT_DOOR_VIEW_TAG];
        [self addSubview:rightDoorView];
        [rightDoorView release];
    }
    return self;
}

#pragma mark -
#pragma mark Set delegate
- (void)setDelegate:(id<DoorViewDelegate>)delegate
{
    if(_delegate != delegate)
    {
        _delegate = delegate;
    }
}

#pragma mark -
#pragma mark Open door
- (void)openDoor
{
    UIImageView *leftDoorView = (UIImageView *)[self viewWithTag:LEFT_DOOR_VIEW_TAG];
    UIImageView *rightDoorView = (UIImageView *)[self viewWithTag:RIGHT_DOOR_VIEW_TAG];
    
    [UIView animateWithDuration:1.0f
                     animations:^{
                         CATransform3D transform3D = CATransform3DMakeRotation(M_PI / 2.0, 0.0f, -1.0f, 0.0f);
                         transform3D.m34 = 0.001f;
                         [leftDoorView.layer setTransform:transform3D];
                         
                         CATransform3D transform3D1 = CATransform3DMakeRotation(M_PI / 2.0, 0.0f, 1.0f, 0.0f);
                         transform3D1.m34 = 0.001f;
                         [rightDoorView.layer setTransform:transform3D1];
                         
                     } completion:^(BOOL finished) {
                         if(_delegate && [_delegate respondsToSelector:@selector(doorView:didDoorOpend:)])
                         {   
                             [_delegate doorView:self didDoorOpend:finished];
                             
                             [self removeFromSuperview];
                         }
                     }];
}

- (void)dealloc
{   
    [super dealloc];
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值