Rotate the image corresponding to the touch drag in iPhone - iphone

I want to rotate the image in clockwise or anticlockwise direction corresponding to the user touch drag with its speed. I think this can be done with some math and logic. What would a code sample for this look like?

If you're targeting iOS 3.2 or greater, you can use UIRotationGestureRecognizer. The code would look something like this:
In your setup method (viewDidLoad or init, etc,):
UIRotationGestureRecognizer *rotationGesture =
[[UIRotationGestureRecognizer alloc] initWithTarget:self
action:#selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];
The event handler:
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
if(recognizer.state == UIGestureRecognizerStateBegan ||
recognizer.state == UIGestureRecognizerStateChanged)
{
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform,
recognizer.rotation);
[recognizer setRotation:0];
}
}
I have some more examples of gesture recognizers (including the one above) on github.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
currentTouch=touch;
if (CGRectContainsPoint([self.view frame], [touch locationInView:self.ViewRotationArrow]))
{
[self transformSpinnerwithTouches:touch];
}
else if (!CGRectContainsPoint([ViewRotationArrow frame], [touch locationInView:self.ViewRotationArrow])) {
[self dispatchTouchEvent:[touch view]WithTouch:touch];
}
}
}
-(void)transformSpinnerwithTouches:(UITouch *)touchLocation
{
CGPoint touchLocationpoint = [touchLocation locationInView:self.view];
CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view];
//Origin is the respective point. From that I am going to measure the
//angle of the current position with respect to the previous position ....
CGPoint origin;
origin.x=240;
origin.y=160;
//NSLog(#"currentTouch Touch In Location In View:%F %F\n",touchLocationpoint.x,touchLocationpoint.y);
//NSLog(#"currentTouch Touch previous Location In View:%F %F\n",PrevioustouchLocationpoint.x,PrevioustouchLocationpoint.y);
CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
CGAffineTransform newTransform =CGAffineTransformScale(ViewRotationArrow.transform, 1, 1);
CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
CGFloat newAngle = currentRotation- previousRotation;
NSLog(#"currentRotation of x %F previousRotation %F\n",currentRotation,previousRotation);
NSLog(#"Angle:%F\n",(temp1*180)/M_PI);
temp1=temp1+newAngle;
//NSLog(#"Angle:%F\n",(temp1*180)/M_PI);
newTransform = CGAffineTransformRotate(newTransform, newAngle);
[self animateView:ViewRotationArrow toPosition:newTransform];
}
-(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
CGPoint result;
CGFloat x = secondPoint.x-firstPoint.x;
CGFloat y = secondPoint.y-firstPoint.y;
result = CGPointMake(x, y);
return result;
}
-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
//animating the rotator arrow...
[UIView setAnimationsEnabled:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.0750];
ViewRotationArrow.transform = newTransform;
[UIView commitAnimations];
}

-(void)rotateView
{
//shape of eclipse
CAShapeLayer* circle = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();
CGRect ViewRect = CGRectMake(self.view.bounds.size.width/2.8, (self.view.bounds.size.height-self.view.bounds.size.width/2)/2, self.view.bounds.size.width/4, self.view.bounds.size.width/2);
float midX = CGRectGetMidX(ViewRect);
float midY = CGRectGetMidY(ViewRect);
CGAffineTransform t = CGAffineTransformConcat(
CGAffineTransformConcat(
CGAffineTransformMakeTranslation(-midX, -midY),
CGAffineTransformMakeRotation(-1.57079633/0.99)),
CGAffineTransformMakeTranslation(midX, midY));
CGPathAddEllipseInRect(path, &t, ViewRect);
circle.path = path;
circle.frame = self.view.bounds;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor greenColor].CGColor;
circle.lineWidth = 3.0f;
[self.view.layer addSublayer:circle];
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:#"strokeEnd"];
animation.duration = 0.0f;
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat:1.0f];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[circle addAnimation:animation forKey:#"strokeEnd"];
// rotateView red color
testView=[[UIView alloc]init];
testView.frame=CGRectMake(0, 0, 20, 20);
testView.backgroundColor=[UIColor redColor];
testView.userInteractionEnabled=YES;
testView.center=CGPathGetCurrentPoint(path);
testView.transform = CGAffineTransformMakeRotation(1.57079633);
[testView sizeToFit];
[self.view.layer addSublayer:testView.layer];
// view Animation
CAKeyframeAnimation* ViewAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
ViewAnimation.duration = 15.0f;
ViewAnimation.path = path;
ViewAnimation.rotationMode = kCAAnimationRotateAuto;
ViewAnimation.calculationMode = kCAAnimationCubicPaced;
//ViewAnimation.removedOnCompletion = NO;
[testView.layer addAnimation:ViewAnimation forKey:#"position"];
}

Related

Touch position highlight

I'm trying to highlight a touch event by using UIGestureRecognizer, so far I can get the CGPoint where the user touched but I don't know how to highlight that specific area since I don't know much about animation.
The code so far on viewDidLoad:
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gradientTouchDown:)];
[self addGestureRecognizer:singleFingerTap];
The method that gets the position of the touch:
-(void)gradientTouchDown:(UIGestureRecognizer*)recognizer{
NSLog(#"tap detected.");
CGPoint point = [recognizer locationInView:nil];
NSLog(#"x = %f y = %f", point.x, point.y );
}
By doing some study I think I need to add the code above with a View animation, but how can I recognize the touch? the code:
[UIView animateWithDuration:0.3f
delay:0.0
options:UIViewAnimationOptionCurveEaseIn
animations:^
{
[sender setAlpha:0.3f];
}
completion:^(BOOL finished)
{
[sender setAlpha:1.0f];
}];
I really need help on this one, any question just ask, I'm always around here :)
This is how I implemented my animation in one of my projects for a button. The animation just puts a glow around the button when pressed (just like the glow you see when you press an year button on the graph view of built in stocks app)
First, add these targets to the button:
[button addTarget:self action:#selector(myButtonPressed:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(myButtonReleased:) forControlEvents:UIControlEventTouchUpInside];
Then these methods looks like this:
- (void) myButtonPressed:(id) sender
{
UIButton *button = (UIButton *) sender;
CABasicAnimation *shadowRadius = [CABasicAnimation animationWithKeyPath:#"shadowRadius" ];
shadowRadius.delegate = self;
[shadowRadius setFromValue:[NSNumber numberWithFloat:3.0]];
[shadowRadius setToValue:[NSNumber numberWithFloat:15.0]];
[shadowRadius setDuration:0.2f];
[[button layer] addAnimation:shadowRadius forKey:#"shadowRadius"];
button.layer.shadowRadius = 15.0f;
}
- (void) myButtonReleased:(id) sender
{
UIButton *button = (UIButton *) sender;
CABasicAnimation *shadowRadius = [CABasicAnimation animationWithKeyPath:#"shadowRadius" ];
shadowRadius.delegate = self;
[shadowRadius setFromValue:[NSNumber numberWithFloat:15.0]];
[shadowRadius setToValue:[NSNumber numberWithFloat:3.0]];
[shadowRadius setDuration:0.2f];
//shadowRadius.autoreverses = YES;
[[button layer] addAnimation:shadowRadius forKey:#"shadowRadius"];
button.layer.shadowRadius = 3.0f;
button.selected = YES;
}
Hi and thanks for the help, I came out with a custom solution for a similar glow effect related to the Apple "Shows Touch on Highlight" here is the code for the glow:
-(void)gradientTouchDown:(UIGestureRecognizer*)recognizer{
NSLog(#"tap detected");
CGPoint point = [recognizer locationInView:nil];
NSLog(#"x = %f y = %f", point.x, point.y );
UIImage* image;
UIColor *color = [UIColor whiteColor];
UIGraphicsBeginImageContextWithOptions(recognizer.view.bounds.size, NO, [UIScreen mainScreen].scale); {
[recognizer.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIBezierPath* path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, recognizer.view.bounds.size.width, recognizer.view.bounds.size.height)];
[color setFill];
[path fillWithBlendMode:kCGBlendModeSourceAtop alpha:1.0];
image = UIGraphicsGetImageFromCurrentImageContext();
} UIGraphicsEndImageContext();
UIView* glowView = [[UIImageView alloc] initWithImage:image];
glowView.center = self.center;
[self.superview insertSubview:glowView aboveSubview:self];
glowView.alpha = 0;
glowView.layer.shadowColor = color.CGColor;
glowView.layer.shadowOffset = CGSizeZero;
glowView.layer.shadowRadius = 10;
glowView.layer.shadowOpacity = 1.0;
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:#"opacity"];
animation.fromValue = #(0.0f);
animation.toValue = #(0.6f);
//animation.repeatCount = 2 ? HUGE_VAL : 0;
animation.duration = 0.2;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[glowView.layer addAnimation:animation forKey:#"pulse"];
}
Any suggestion for improvement is welcome, so far it works for me :)

Image rotate and scale

I need to rotate and scale a UIImageView image with the help of UISlider.
There are three condition when UISlider value is in its middle position then the original image will be its original position.
Second case, When slider value is maximum then image will rotate with 45 degree add bit scale.
Third case, when slider value is in its minimum position then image will rotate 45 degree in opposite direction and also bit scale.
I used this code but did not get desired result.
-(IBAction)sliderMoved:(id)sender
{
UIImage *image = [UIImage imageNamed:#"landscape.jpg"];
photoImage.transform = CGAffineTransformMakeRotation(slider.value * 2*M_PI_2 / slider.maximumValue);
CGFloat scale=0.5;
NSLog(#"sliderVlaue=%f",slider.value);
if ((slider.value) >(0.75)) {
scale =(.25+ slider.value);
} else {
scale =(0.75 +slider.value);
}
CGAffineTransform currentTransform = photoImage.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[photoImage setTransform:newTransform];
}
you can try this code
CGFloat Angle=(((int)slider.value*M_PI)/180);
transform=CGAffineTransformScale(transform, photoImage.scale, photoImage.scale);
transform= CGAffineTransformRotate(transform,Angle);
photoImage.transform = transform;
You can use this code for image rotation
imageview.transform = CGAffineTransformMakeRotation(M_PI);
For scaling an image
-(IBAction)grow
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:YES];
CGRect b = grower.bounds;
b.size.height = 200;
b.size.width = 200;
grower.bounds = b;
[UIView commitAnimations];
}
I tried my self with the help of given hints and able to way out from my problem. here is the code`
in you h file declare UIImageView *photoImage;
UIView *canvas;
CAShapeLayer *_marque;
And in view did load
if (!_marque) {
_marque = [CAShapeLayer layer] ;
_marque.fillColor = [[UIColor clearColor] CGColor];
_marque.strokeColor = [[UIColor grayColor] CGColor];
_marque.lineWidth = 1.0f;
_marque.lineJoin = kCALineJoinRound;
_marque.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];
_marque.bounds = CGRectMake(photoImage.frame.origin.x, photoImage.frame.origin.y, 0, 0);
_marque.position = CGPointMake(photoImage.frame.origin.x + canvas.frame.origin.x, photoImage.frame.origin.y + canvas.frame.origin.y);
}
[[self.view layer] addSublayer:_marque];
- (IBAction) sliderMoved:(id)sender
{
CGFloat scale=0;
NSLog(#"sliderVlaue=%f",slider.value);
if ((slider.value) >(0.77)) {
scale =(.5+ slider.value);
photoImage.transform = CGAffineTransformMakeRotation(((4*slider.value)*(M_PI/180)));
CGAffineTransform currentTransform = photoImage.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale-.25, scale-.25);
[photoImage setTransform:newTransform];
NSLog(#"max=%f",scale);
}else if(slider.value< 0.77 && slider.value >.73){
photoImage.transform=CGAffineTransformIdentity;
photoImage.frame=imageframe;
scale =(0.5 +slider.value);
NSLog(#"mid=%f",scale);
}
else{
scale =(.77 +(1.0-slider.value));
photoImage.transform = CGAffineTransformMakeRotation((((-4)*(slider.value))*(M_PI/180)));
CGAffineTransform currentTransform = photoImage.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[photoImage setTransform:newTransform];
NSLog(#"min=%f",scale);
}
//_lastScale = 1;
[self showOverlayWithFrame:photoImage.frame];
}
-(void)showOverlayWithFrame:(CGRect)frame {
if (![_marque actionForKey:#"linePhase"]) {
CABasicAnimation *dashAnimation;
dashAnimation = [CABasicAnimation animationWithKeyPath:#"lineDashPhase"];
[dashAnimation setFromValue:[NSNumber numberWithFloat:0.0f]];
[dashAnimation setToValue:[NSNumber numberWithFloat:15.0f]];
[dashAnimation setDuration:0.5f];
[dashAnimation setRepeatCount:HUGE_VALF];
[_marque addAnimation:dashAnimation forKey:#"linePhase"];
}
_marque.bounds = CGRectMake(frame.origin.x, frame.origin.y, 0, 0);
_marque.position = CGPointMake(frame.origin.x + canvas.frame.origin.x, frame.origin.y + canvas.frame.origin.y);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, frame);
[_marque setPath:path];
CGPathRelease(path);
_marque.hidden = NO;
}
You can do it with the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
imgToRotate = [[UIImageView alloc]initWithFrame:CGRectMake(48, 208, 240, 240)];
imgToRotate.image = [UIImage imageNamed:#"rtm.jpeg"];
[self.view addSubview:imgToRotate];
}
- (IBAction)slideToRotateScale:(UISlider *)sender
{
sender.maximumValue = 1.0;
sender.minimumValue = -1.0;
imgToRotate.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(fabsf(sender.value), fabs(sender.value)), sender.value*M_PI/4);
}

Transform not working

I am trying to rotate an object around it's current position,
CGAffineTransform trans = CGAffineTransformMakeRotation(compass_heading );
trans = CGAffineTransformTranslate(trans, 0, 90);
sun_image.transform = trans;
But it has no effect and sun_image doesn't move!
But if I change it to this and translate on X axis it moves! What does this mean?
CGAffineTransform trans = CGAffineTransformMakeRotation(compass_heading );
trans = CGAffineTransformTranslate(trans, 90, 0);
sun_image.transform = trans;
Here is how you can rotate an object:
CGFloat rotation = M_PI_2;
CGAffineTransform currentTransform = sun_image.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[sun_image setTransform:newTransform];
With CGAffineTransformTranslate you can move an object, so you must use CGAffineTransformRotate.
For location manager :
-(void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
//set the anchorPoint as the lower right corner of the layer
sun_image.layer.anchorPoint = CGPointMake(1, 1);
[sun_image.layer setAffineTransform:CGAffineTransformMakeRotation(-1*newHeading.trueHeading*3.14159/180)];
}
I generally do this for translation transforms:
sun_image.transform = CGAffineTransformMakeTranslation(dx,dy);
First set center of your UIImageView with your view center property like bellow..
yourImageView.center = self.view.center;
after set GestureRecognizer like bellow..
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[yourImageView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
and then use this bellow method for rotate UIImageView
-(void)rotate:(id)sender
{
UIView *imgTempGest = [sender view];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
[imgTempGest setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
take lastRotation variable of CGFloat like bellow..
CGFloat lastRotation;

iOS Paper fold (origami / accordion) effect animation, with manual control

I'm looking for tips on how to implement the popular 'paper folding / origami' effect in my iOS project.
I'm aware of projects such as:
https://github.com/xyfeng/XYOrigami
but they only offer the 'animated' effect, with no manual control over the opening animation.
I've struggled to dissect that project and come up with what I'm after.
To be more exact, I'm looking on how to implement the effect shown here: http://vimeo.com/41495357 where the folding animation is not simply animated open, but the user controls the opening folds.
Any help would be much appreciated, thanks in advance!
EDIT:
Okay, here's some example code to better illustrate what I'm struggling with:
This method triggers the origami effect animation:
- (void)showOrigamiTransitionWith:(UIView *)view
NumberOfFolds:(NSInteger)folds
Duration:(CGFloat)duration
Direction:(XYOrigamiDirection)direction
completion:(void (^)(BOOL finished))completion
{
if (XY_Origami_Current_State != XYOrigamiTransitionStateIdle) {
return;
}
XY_Origami_Current_State = XYOrigamiTransitionStateUpdate;
//add view as parent subview
if (![view superview]) {
[[self superview] insertSubview:view belowSubview:self];
}
//set frame
CGRect selfFrame = self.frame;
CGPoint anchorPoint;
if (direction == XYOrigamiDirectionFromRight) {
selfFrame.origin.x = self.frame.origin.x - view.bounds.size.width;
view.frame = CGRectMake(self.frame.origin.x+self.frame.size.width-view.frame.size.width, self.frame.origin.y, view.frame.size.width, view.frame.size.height);
anchorPoint = CGPointMake(1, 0.5);
}
else {
selfFrame.origin.x = self.frame.origin.x + view.bounds.size.width;
view.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, view.frame.size.width, view.frame.size.height);
anchorPoint = CGPointMake(0, 0.5);
}
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewSnapShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//set 3D depth
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0/800.0;
CALayer *origamiLayer = [CALayer layer];
origamiLayer.frame = view.bounds;
origamiLayer.backgroundColor = [UIColor colorWithWhite:0.2 alpha:1].CGColor;
origamiLayer.sublayerTransform = transform;
[view.layer addSublayer:origamiLayer];
//setup rotation angle
double startAngle;
CGFloat frameWidth = view.bounds.size.width;
CGFloat frameHeight = view.bounds.size.height;
CGFloat foldWidth = frameWidth/(folds*2);
CALayer *prevLayer = origamiLayer;
for (int b=0; b < folds*2; b++) {
CGRect imageFrame;
if (direction == XYOrigamiDirectionFromRight) {
if(b == 0)
startAngle = -M_PI_2;
else {
if (b%2)
startAngle = M_PI;
else
startAngle = -M_PI;
}
imageFrame = CGRectMake(frameWidth-(b+1)*foldWidth, 0, foldWidth, frameHeight);
}
else {
if(b == 0)
startAngle = M_PI_2;
else {
if (b%2)
startAngle = -M_PI;
else
startAngle = M_PI;
}
imageFrame = CGRectMake(b*foldWidth, 0, foldWidth, frameHeight);
}
CATransformLayer *transLayer = [self transformLayerFromImage:viewSnapShot Frame:imageFrame Duration:duration AnchorPiont:anchorPoint StartAngle:startAngle EndAngle:0];
[prevLayer addSublayer:transLayer];
prevLayer = transLayer;
}
[CATransaction begin];
[CATransaction setCompletionBlock:^{
self.frame = selfFrame;
[origamiLayer removeFromSuperlayer];
XY_Origami_Current_State = XYOrigamiTransitionStateShow;
if (completion)
completion(YES);
}];
[CATransaction setValue:[NSNumber numberWithFloat:duration] forKey:kCATransactionAnimationDuration];
CAAnimation *openAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position.x" function:openFunction fromValue:self.frame.origin.x+self.frame.size.width/2 toValue:selfFrame.origin.x+self.frame.size.width/2];
openAnimation.fillMode = kCAFillModeForwards;
[openAnimation setRemovedOnCompletion:NO];
[self.layer addAnimation:openAnimation forKey:#"position"];
[CATransaction commit];
}
The method grabs a CATransform Layer from this method:
- (CATransformLayer *)transformLayerFromImage:(UIImage *)image Frame:(CGRect)frame Duration:(CGFloat)duration AnchorPiont:(CGPoint)anchorPoint StartAngle:(double)start EndAngle:(double)end;
{
CATransformLayer *jointLayer = [CATransformLayer layer];
jointLayer.anchorPoint = anchorPoint;
CGFloat layerWidth;
if (anchorPoint.x == 0) //from left to right
{
layerWidth = image.size.width - frame.origin.x;
jointLayer.frame = CGRectMake(0, 0, layerWidth, frame.size.height);
if (frame.origin.x) {
jointLayer.position = CGPointMake(frame.size.width, frame.size.height/2);
}
else {
jointLayer.position = CGPointMake(0, frame.size.height/2);
}
}
else
{ //from right to left
layerWidth = frame.origin.x + frame.size.width;
jointLayer.frame = CGRectMake(0, 0, layerWidth, frame.size.height);
jointLayer.position = CGPointMake(layerWidth, frame.size.height/2);
}
//map image onto transform layer
CALayer *imageLayer = [CALayer layer];
imageLayer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
imageLayer.anchorPoint = anchorPoint;
imageLayer.position = CGPointMake(layerWidth*anchorPoint.x, frame.size.height/2);
[jointLayer addSublayer:imageLayer];
CGImageRef imageCrop = CGImageCreateWithImageInRect(image.CGImage, frame);
imageLayer.contents = (__bridge id)imageCrop;
imageLayer.backgroundColor = [UIColor clearColor].CGColor;
//add shadow
NSInteger index = frame.origin.x/frame.size.width;
double shadowAniOpacity;
CAGradientLayer *shadowLayer = [CAGradientLayer layer];
shadowLayer.frame = imageLayer.bounds;
shadowLayer.backgroundColor = [UIColor darkGrayColor].CGColor;
shadowLayer.opacity = 0.0;
shadowLayer.colors = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
if (index%2) {
shadowLayer.startPoint = CGPointMake(0, 0.5);
shadowLayer.endPoint = CGPointMake(1, 0.5);
shadowAniOpacity = (anchorPoint.x)?0.24:0.32;
}
else {
shadowLayer.startPoint = CGPointMake(1, 0.5);
shadowLayer.endPoint = CGPointMake(0, 0.5);
shadowAniOpacity = (anchorPoint.x)?0.32:0.24;
}
[imageLayer addSublayer:shadowLayer];
//animate open/close animation
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.y"];
[animation setDuration:duration];
[animation setFromValue:[NSNumber numberWithDouble:start]];
[animation setToValue:[NSNumber numberWithDouble:end]];
[animation setRemovedOnCompletion:NO];
[jointLayer addAnimation:animation forKey:#"jointAnimation"];
//animate shadow opacity
animation = [CABasicAnimation animationWithKeyPath:#"opacity"];
[animation setDuration:duration];
[animation setFromValue:[NSNumber numberWithDouble:(start)?shadowAniOpacity:0]];
[animation setToValue:[NSNumber numberWithDouble:(start)?0:shadowAniOpacity]];
[animation setRemovedOnCompletion:NO];
[shadowLayer addAnimation:animation forKey:nil];
return jointLayer;
}
Basically, I need to remove the automatic animation, and control the progress of the effect using some manually set value (e.g.: uislider value, or content offset).
Once again, any help provided is much appreciated!
I write another library for folding transition : https://github.com/geraldhuard/YFoldView
Hope it works for you.
Try this. You have drag control over the paper fold, left and right side.
https://github.com/honcheng/PaperFold-for-iOS
This is the best solution I've seen:
http://api.mutado.com/mobile/paperstack/
EDIT: What about this: the paper folding/unfolding effect in twitter for iPad

How to rotate UIButton or UIImage view that follows finger (Touch and hold)?

I need some help.How to rotate UIButton or UIImageView that follows finger (Touch and hold, UILongPressGestureRecognizer)? Thx 4 help
UPD: Don't understand what i'm doing wrong?
- (void)viewDidLoad {
UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.view addGestureRecognizer:tapgr];
[tapgr release];
[super viewDidLoad];
}
-(void)tap:(UITapGestureRecognizer *)gesture {
CGPoint touch = [gesture locationInView:self.view];
CGPoint center = myImage.center;
float dx,dy,wtf;
dx = touch.x-center.x;
dy = touch.y-center.y;
wtf = atan2f(dy, dx);
[self rotateImage:self.myImage withAngle:wtf];
}
- (void)rotateImage:(UIImageView *)image withAngle:(float)newAngle
{
image.transform = CGAffineTransformMakeRotation(newAngle);
}
Glad I remember triginometry
-(void)degreesToRotateObjectWithPosition:(CGPoint)objPos andTouchPoint:(CGPoint)touchPoint{
float dX = touchPoint.x-objPos.x; // distance along X
float dY = touchPoint.y-objPos.y; // distance along Y
float radians = atan2(dY, dX); // tan = opp / adj
//Now we have to convert radians to degrees:
float degrees = radians*M_PI/360;
return degrees;
}
Once you have your nice method, just do this in the touch event method. (I forgot what it's called...)
CGAffineTransform current = view.transform;
[view setTransform:CGAffineTransformRotate(current, [self degreesTorotateObjectWithPosition:view.frame.origin andTouchPoint:[touch locationInView:parentView]]
//Note: parentView = The view that your object to rotate is sitting in.
This is pretty much all the code that you'll need.The math is right, but I'm not sure about the setTransform stuff. I'm at school writing this in a browser. You should be able to figure it out from here.
Good luck,
Aurum Aquila
- (void) LongPress:(UILongPressGestureRecognizer *)gesture {
CGPoint p = [gesture locationInView:self.view];
CGPoint zero;
zero.x = self.view.bounds.size.width / 2.0;
zero.y = self.view.bounds.size.height / 2.0;
CGPoint newPoint;
newPoint.x = p.x - zero.x;
newPoint.y = zero.y - p.y;
angle = atan2(newPoint.x, newPoint.y);
UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState|UIViewAnimationOptionAllowUserInteraction;
[UIView animateWithDuration:0.2 delay:0.0 options:options
animations:^{myButton.transform = CGAffineTransformRotate(CGAffineTransformIdentity, angle);}
completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 delay:0.5 options:options animations:^{
myButton.transform = CGAffineTransformRotate(CGAffineTransformIdentity, [self detectQuarter:angle]);
} completion:nil];
}];
}
#define M_PI_3_4 3*M_PI_4
-(CGFloat)detectQuarter:(CGFloat)anAngle {
if ((anAngle >= -M_PI_4)&&(anAngle <= M_PI_4)) return 0;
if ((anAngle > M_PI_4) && (anAngle <= 3*M_PI_4)) return M_PI_2;
if ((anAngle >= -M_PI_3_4) && (anAngle < -M_PI_4)) return -M_PI_2;
else return M_PI;
}