Skip to content

Instantly share code, notes, and snippets.

@Shosta
Last active December 20, 2015 18:18
Show Gist options
  • Save Shosta/6174826 to your computer and use it in GitHub Desktop.
Save Shosta/6174826 to your computer and use it in GitHub Desktop.
Allow the developer to rotate View during a predefined time or start an infinite rotation and stop it.
//--------------------------------------------------------
//
//--------------------------------------------------------
// Project :
// File : UIView+Rotation.h
// Created : $ 23/05/12 $
// Maintainer : $ Rémi LAVEDRINE $
//
// Copyright Rémi LAVEDRINE 2004-2012, All Rights Reserved
//
// This software is the confidential and proprietary
// information of Rémi LAVEDRINE.
// You shall not disclose such Confidential Information
// and shall use it only in accordance with the terms
// of the license agreement you entered into with
// Rémi LAVEDRINE.
//--------------------------------------------------------
//
// @brief
//
#import <UIKit/UIKit.h>
@interface UIView (Rotation)
- (void)rotate:(CGFloat)angle during:(NSTimeInterval)duration;
- (void)infiniteRotation;
- (void)stopInfiniteRotation;
@end
//
// UIView+Rotation.m
//
//
// Created by Rémi LAVEDRINE on 23/05/12.
// Copyright (c) 2012 Rémi LAVEDRINE. All rights reserved.
//
#import "UIView+Rotation.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (Rotation)
- (void)rotate:(CGFloat)angle during:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
self.transform = transform;
// Commit the changes
[UIView commitAnimations];
// Animate squash
}completion:^(BOOL finished){
if (finished) {
// [[NSNotificationCenter defaultCenter] postNotificationName:kRotationDone object:nil];
}
}];
}
- (void)rotateToValue:(NSNumber *)value during:(NSTimeInterval)duration cumulative:(BOOL)isCumulative repeatCount:(CGFloat)repeatCount{
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = value;
rotationAnimation.duration = duration;
rotationAnimation.cumulative = isCumulative;
rotationAnimation.repeatCount = repeatCount;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
#define ONE_ROTATION_DURATION 1.0
/**
@brief Rotate through an infinite loop in trigonometrical direction.
@author : Rémi Lavedrine
@date : 23/05/2012
@remarks : <#(optional)#>
*/
- (void)infiniteRotation{
[self rotateToValue:[NSNumber numberWithFloat: -M_PI * 2.0 /* full rotation*/ * 1 * 1.0 ]
during:ONE_ROTATION_DURATION
cumulative:YES
repeatCount:MAXFLOAT];
}
/**
@brief Stop the loop.
@author : Rémi Lavedrine
@date : 23/05/2012
@remarks : <#(optional)#>
*/
- (void)stopInfiniteRotation {
[self rotateToValue:[NSNumber numberWithFloat: -M_PI * 2.0 /* full rotation*/ * 1 * 1.0 ]
during:ONE_ROTATION_DURATION
cumulative:NO
repeatCount:0];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment