美文网首页
拖动图片旋转和音乐条

拖动图片旋转和音乐条

作者: cj小牛 | 来源:发表于2020-03-03 22:36 被阅读0次
//
//  ViewController.m
//  testcli
//
//  Created by Jie Chen on 2017/8/1.
//  Copyright © 2017年 Jie Chen. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView *bottonImageView;
@property(strong,nonatomic)CAGradientLayer * gradLayer;
@property (weak, nonatomic) IBOutlet UIView *bgView;

@end

 @implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

[self suofang];


[self.view insertSubview:self.bottonImageView belowSubview:self.imageView];

self.imageView.layer.contentsRect = CGRectMake(0, 0, 1.0, 0.5);
self.imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);
self.bottonImageView.layer.contentsRect = CGRectMake(0, 0.5, 1.0, 0.5);
self.bottonImageView.layer.anchorPoint = CGPointMake(0.5, 0);

}

-(void)yingYueTiao{
CAReplicatorLayer * replicatorLayer = [CAReplicatorLayer layer];
replicatorLayer.frame = self.bgView.bounds;
[self.bgView.layer addSublayer:replicatorLayer];
CALayer *  layer = [CALayer layer];
layer.anchorPoint = CGPointMake(0, 1);
layer.frame = CGRectMake(0, self.bgView.frame.size.height -60, 30, 60);
layer.backgroundColor = [UIColor redColor].CGColor;
[replicatorLayer addSublayer:layer];
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
animation.toValue = @(0.0);
//    animation.fromValue= @(0.0);
//    animation.values=@[];
animation.duration = 1.0;
animation.autoreverses = YES;

animation.repeatCount = MAXFLOAT;

[layer addAnimation:animation forKey:nil];
//复制的延迟时间
replicatorLayer.instanceDelay = 1;
// 复制的个数
replicatorLayer.instanceCount  = 5;
// 每个之间距离 包含宽度
replicatorLayer.instanceTransform = CATransform3DMakeTranslation(45, 0, 0);
}

//渐变色的layer

-(CAGradientLayer*)gradLayer{
if (!_gradLayer) {
    _gradLayer = [CAGradientLayer layer];
    _gradLayer.frame = self.bottonImageView.bounds;
//    _gradLayer.colors = @[(id)[UIColor redColor].CGColor ,(id)[UIColor orangeColor].CGColor,(id)[UIColor greenColor].CGColor];
//   _gradLayer.locations=@[@(0.2)];
    _gradLayer.startPoint = CGPointMake(1, 0);
    _gradLayer.endPoint = CGPointMake(1, 1);
    [self.bottonImageView.layer addSublayer:_gradLayer];
}

return _gradLayer;
}
- (IBAction)pan:(UIPanGestureRecognizer*)pan {


CGPoint  testPoint = [pan translationInView:pan.view];

NSLog(@"%@",NSStringFromCGPoint(testPoint));
CGFloat  angle = testPoint.y / 320 * M_PI;

CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0 /500;
//  [UIView animateWithDuration:0.5 animations:^{
//   self.imageView.layer.transform = CATransform3DMakeRotation(-angle, 1, 0, 0);
//  }];

self.gradLayer.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
self.gradLayer.opacity= testPoint.y/320;

-angle 这个的正负是用来控制方向的

self.imageView.layer.transform = CATransform3DRotate(transform, -angle, 1, 0, 0)
if (pan.state == UIGestureRecognizerStateEnded) {
    
    self.gradLayer.opacity = 0;
    [UIView animateWithDuration:0.5 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear    animations:^{
        self.imageView.layer.transform= CATransform3DIdentity;
    } completion:^(BOOL finished) {
        
    }];
    
}
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent 
  *)event{

//    [UIView animateWithDuration:0.5 animations:^{
//      self.imageView.layer.transform = CATransform3DMakeRotation(M_PI, 1, 0, 0);
//    }];

}




@end

相关文章

网友评论

      本文标题:拖动图片旋转和音乐条

      本文链接:https://www.haomeiwen.com/subject/tlccdxtx.html