IOS 实现 UIView 晃动 删除的 效果

// 方法一:



           
//
//  ViewController.h
//  text02
//
//  Created by YQ - 014 on 9/3/12.
//  Copyright (c) 2012 YQ - 014. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIView *handle;


@property (retain, nonatomic) IBOutlet UIButton *deleteButton;

- (IBAction)deletePressed:(id)sender;

- (IBAction)cancel:(id)sender;

- (IBAction)buttonPressed:(id)sender;
@end




@interface UIView (MyExtension)
- (void)wobble:(BOOL)wobble;
@end




//
//  ViewController.m
//  text02
//
//  Created by YQ - 014 on 9/3/12.
//  Copyright (c) 2012 YQ - 014. All rights reserved.
//

#import "ViewController.h"

@implementation  UIView (MyExtension)

- (void)wobble:(BOOL)wobble
{
    if (wobble){ 
        self.transform = CGAffineTransformMakeRotation(-0.1);

        [UIView animateWithDuration:0.12
                              delay:0.0
                            options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveLinear
                         animations:^{    
                             self.transform = CGAffineTransformMakeRotation(0.1);
                         } completion:nil];
    }
    else{
        
        [UIView animateWithDuration:0.25
                              delay:0.0
                            options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             self.transform = CGAffineTransformIdentity;
                         } completion:nil];
    }
}

@end



@interface ViewController ()

@end

@implementation ViewController
@synthesize handle;
@synthesize deleteButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    deleteButton.enabled = NO;
    deleteButton.hidden = YES;
}

- (void)viewDidUnload
{
    [self setHandle:nil];
    [self setDeleteButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [handle release];
    [deleteButton release];
    [super dealloc];
}
- (IBAction)deletePressed:(id)sender {
    [self.handle removeFromSuperview];
}

- (IBAction)cancel:(id)sender {
    deleteButton.enabled = NO;
    deleteButton.hidden = YES;
    [handle wobble:NO];
}

- (IBAction)buttonPressed:(id)sender {
    deleteButton.hidden = NO;
    deleteButton.enabled = YES;
    [handle wobble:YES];
}
@end


// 方法二:

CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform"];
    shake.duration = 0.13;
    shake.autoreverses = YES;
    shake.repeatCount = MAXFLOAT;
    shake.removedOnCompletion = NO;
    CGFloat rotation = 0.03;
    shake.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,-rotation, 0.0 ,0.0 ,1.0)];
    shake.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform, rotation, 0.0 ,0.0 ,1.0)];
    
    [self.layer addAnimation:shake forKey:@"shakeAnimation"];



// 效果 如下





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值