开门动画

来源:互联网 发布:apache调试选项 编辑:程序博客网 时间:2022/08/11 17:23

使用方式:

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];}