ios激情详解之动画3D旋转晃动

13 篇文章 0 订阅
//
//  Created by WDX on 15/9/23.
//  Copyright (c) 2015年 WDongXu. All rights reserved.
//
#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic, retain)UIView *myView;


@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addSubViews];
   
}
// 初始化 创建视图
- (void)addSubViews
{
    self.myView = [[UIView alloc]initWithFrame:(CGRectMake(100, 100, 100, 100))];
    self.myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.myView];
    [_myView release];
    
    // layer是负责显示图层的
    // 要更改 咱们看到的图形的形状 需要更该layer
    
    // 设置圆角
    // 先决条件 变圆 必须是长宽相同
    self.myView.layer.cornerRadius = self.myView.frame.size.width / 2;
    // 设置阴影的颜色
    // CGColorRef 图层绘制的颜色
    self.myView.layer.shadowColor = [UIColor blackColor].CGColor;
    // 显示阴影范围
    self.myView.layer.shadowOffset = CGSizeMake(10, 10);
    // 阴影透明度
    self.myView.layer.shadowOpacity = 1;
    // 模糊程度
    self.myView.layer.shadowRadius = 50;
    // 设置边框
    self.myView.layer.borderWidth = 10;
    // 设置边框的颜色
    self.myView.layer.borderColor = [UIColor blueColor].CGColor;
    // layer层动画
    
    // CAKeyframeAnimation抽象类
    // CABasicAnimation 基础动画 可以更改大小,旋转等
    // CAPropertyAnimation 主要有按照轨迹移动,位置 比如 执行一组动画是使用背景颜色
    
// 创建一个button
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(100, 300, 100, 100);
                    button.backgroundColor = [UIColor blueColor];
                    [self.view addSubview:button];
                    [button setTitle:@"点我" forState:(UIControlStateNormal)];
                    [button addTarget:self action:@selector(actionButton:) forControlEvents:(UIControlEventTouchUpInside)];
}
- (void)actionButton:(UIButton *)button
 {
     //  [self xyAnimation];
     //[self sizeAnimation];
     //[self changeBackgroundColor];
     // [self positionPoint];
      [self huangDongPoint];
     [self transform3D];
     // [self groupAnimation];
}
// 实现晃动
- (void)huangDongPoint
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    CGFloat center = self.myView.layer.position.x;
    CGFloat left = center - 10;
    CGFloat right = center + 10;
    NSNumber *l = [NSNumber numberWithFloat:left];
    NSNumber *c = [NSNumber numberWithFloat:center];
    NSNumber *r = [NSNumber numberWithFloat:right];
    animation.values = @[l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c,r,l,c];
    animation.duration = 1;

    [self.myView.layer addAnimation:animation forKey:@"huangDong.x"];
}
// 3d旋转
- (void)transform3D
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    // 结束值
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.myView.layer.transform, M_PI, 10, 10, 10)];
    // 设置时间
    animation.duration = 2;
    [self.myView.layer addAnimation:animation forKey:@"transform"];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import UIKit import QuartzCore import SceneKit class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // create a new scene let scene = SCNScene(named: "art.scnassets/ship.scn")! // create and add a camera to the scene let cameraNode = SCNNode() cameraNode.camera = SCNCamera() scene.rootNode.addChildNode(cameraNode) // place the camera cameraNode.position = SCNVector3(x: 0, y: 0, z: 15) // create and add a light to the scene let lightNode = SCNNode() lightNode.light = SCNLight() lightNode.light!.type = SCNLightTypeOmni lightNode.position = SCNVector3(x: 0, y: 10, z: 10) scene.rootNode.addChildNode(lightNode) // create and add an ambient light to the scene let ambientLightNode = SCNNode() ambientLightNode.light = SCNLight() ambientLightNode.light!.type = SCNLightTypeAmbient ambientLightNode.light!.color = UIColor.darkGrayColor() scene.rootNode.addChildNode(ambientLightNode) // retrieve the ship node let ship = scene.rootNode.childNodeWithName("ship", recursively: true)! // animate the 3d object ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1))) // retrieve the SCNView let scnView = self.view as! SCNView // set the scene to the view scnView.scene = scene // allows the user to manipulate the camera scnView.allowsCameraControl = true // show statistics such as fps and timing information scnView.showsStatistics = true // configure the view scnView.backgroundColor = UIColor.blackColor() // add a tap gesture recognizer let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:") scnView.addGestureRecognizer(tapGesture) } func handleTap(gestureRecognize: UIGestureRecognizer) { // retrieve the SCNView let scnView = self.view as! SCNView // check what nodes are tapped let p = gestureRecognize.locationInView(scnView) let hitResults = scnView.hitTest(p, options: nil) // check that we clicked on at least one object if hitResults.count > 0 { // retrieved the first clicked object let result: AnyObject! = hitResults[0] // get its material let material = result.node!.geometry!.firstMaterial! // highlight it SCNTransaction.begin() SCNTransaction.setAnimationDuration(0.5) // on completion - unhighlight SCNTransaction.setCompletionBlock { SCNTransaction.begin() SCNTransaction.setAnimationDuration(0.5) material.emission.contents = UIColor.blackColor() SCNTransaction.commit() } material.emission.contents = UIColor.redColor() SCNTransaction.commit() } } override func shouldAutorotate() -> Bool { return true } override func prefersStatusBarHidden() -> Bool { return true } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { if UIDevice.currentDevice().userInterfaceIdiom == .Phone { return .AllButUpsideDown } else { return .All } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Release any cached data, images, etc that aren't in use. } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值