Hi in my application i have array of images i want to view image by swipe so i have used the Swipe Gesture in my application for both left and right but the problem is its not showing the images in order its just showing two images only please tell me how to resolve this issue.
My swipe code.
- (IBAction)handleswipe:(UIGestureRecognizer *)sender {
//NSLog(#"swiped");
int imageIndex = 3;
NSArray *images = [[NSArray alloc] initWithObjects:
#"2.jpg",
#"3.jpg",
#"4.jpg",
#"5.jpg", nil];
UISwipeGestureRecognizerDirection direction = [(UISwipeGestureRecognizer *) sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
break;
default:
break;
}
imageIndex = (imageIndex < 0 )? ([images count] -1):
imageIndex % [images count];
imageview.image = [UIImage imageNamed:[images objectAtIndex:imageIndex]];
}
I have added the Swipe Gesture in image view and i have set one image already in imageview but its not showing in order its only showing two images in randomly please tell me where I'm doing wrong and how to resolve this one.
Thanks.
right_gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(method)];
right_gesture.direction = UISwipeGestureRecognizerDirectionLeft;
[objSecondView.view addGestureRecognizer:right_gesture];
right_gesture.delegate = self;
and in method...
if (varSecondCount==3) {
}
else{
varSecondCount++;
switch (varSecondCount) {
case 0:
[imageView setImage:[UIImage imageNamed:#"rcz_img1.png"]];
break;
case 1:
[imageView setImage:[UIImage imageNamed:#"rcz_img2.png"]];
break;
case 2:
[imageView setImage:[UIImage imageNamed:#"rcz_img3.png"]];
break;
case 3:
[imageView setImage:[UIImage imageNamed:#"rcz_img4.png"]];
break;
case 4:
[imageView setImage:[UIImage imageNamed:#"step2_5.png"]];
break;
default:
break;
}
hope this helps....
I have implemented same thing in my app here is code for that: imageIndxMain is global variable holding current index of array
if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
{
if (imageIndxMain==0)
return;
else
{
CATransition *transition = [CATransition animation];
transition.duration = .40;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[imagescrollview.layer addAnimation:transition forKey:nil];
UIImage *img1;
imageIndxMain--;
ImgView.image=[imageArray objectAtIndex:imageIndxMain];
}}else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
{
if (imageIndxMain == ([imageArray count] - 1))
{
return;
}
else
{
CATransition *transition = [CATransition animation];
transition.duration = .40;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
transition.delegate = self;
[imagescrollview.layer addAnimation:transition forKey:nil];
UIImage *img1;
imageIndxMain++;
ImgView.image=[imageArray objectAtIndex:imageIndxMain];
}
}
Related
I have one image. I want to spin it like a coin spin on surface. I tried rotation transform but it does not spin like that. How to achieve such an animation?
code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
lbl_facebook.font=[UIFont fontWithName:GZFont size:12.0f];
txtPassword.font=[UIFont fontWithName:GZFont size:15.0f];
txtUsername.font=[UIFont fontWithName:GZFont size:15.0f];
CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = #"flip";
transition.subtype = #"fromRight";
transition.duration = 0.3;
transition.repeatCount = 2;
[_Image.layer addAnimation:transition forKey:#"transition"];
}
nd:
#import "LoginViewController.h"
#import "RegistrationViewController.h"
#import "ForgetPasswordViewController.h"
#import "ForgetPasswordController.h"
#import "SearchServiceProviderViewController.h"
#import <QuartzCore/QuartzCore.h>
This will make a nice, coin-like flip:
CATransition* transition = [CATransition animation];
transition.startProgress = 0;
transition.endProgress = 1.0;
transition.type = #"flip";
transition.subtype = #"fromRight";
transition.duration = 0.3;
transition.repeatCount = 2;
And add the transition animation to the layer of your view:
[_yourView.layer addAnimation:transition forKey:#"transition"];
See it in action:
One good trick to spin like this is that take different images of coin with different angle like spinning image. And then add all those images to array and start animation of images with that array..it will give you much better effect...Simple process like video framing.
Like:
NSArray *animationArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"images.jpg"],
[UIImage imageNamed:#"images1.jpg"],
[UIImage imageNamed:#"images5.jpg"],
[UIImage imageNamed:#"index3.jpg"],
nil];
UIImageView *animationView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
animationView.backgroundColor = [UIColor purpleColor];
animationView.animationImages = animationArray;
animationView.animationDuration = 1.5;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
I have a set of images which i want to display as a slideshow.Is there any transition effects i can apply on UIImageView.
[imgView setImage:[UIImage imageNamed: #"images.jpeg"]];
Yes you can apply transition effects on UIImageView.
First you need to import QuartzCore framework.
#import <QuartzCore/QuartzCore.h>
Then you can use the CoreAnimation as below:
[imgView setImage:[UIImage imageNamed: #"images.jpeg"]];
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[imgView.layer addAnimation:transition forKey:nil];
Or you can simply animate a group of images in UIImageView as
imgView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.jpeg"],
[UIImage imageNamed:#"image2.jpeg"],
[UIImage imageNamed:#"image3.jpeg"],
[UIImage imageNamed:#"image4.jpeg"], nil];
imgView.animationDuration = 1.0f;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
Here is a sample code of how to create a slide show. Make sure you import some images unto your project and in the case method change the wall images to the name of your images.
- (IBAction)start:(id)sender {
_button.hidden = YES;
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(photoCounter) userInfo:nil repeats:YES];
}
- (void)photoCounter {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.90];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO];
[self updatePhoto];
[UIView commitAnimations];
}
- (void)updatePhoto
{
switch (imageCount)
{
case 0:
_images.image = [UIImage imageNamed:#"wall1.jpg"];
break;
case 1:
_images.image = [UIImage imageNamed:#"wall2.jpg"];
break;
case 2:
_images.image = [UIImage imageNamed:#"wall3.jpg"];
break;
case 3:
_images.image = [UIImage imageNamed:#"wall4.jpg"];
break;
case 4:
_images.image = [UIImage imageNamed:#"wall5.jpg"];
break;
default:
break;
}
imageCount ++;
if (imageCount > 4)
imageCount = 0;
}
Try with below code:
NSArray *imageNames = #[#"2_1014.png", #"2_1015.png", #"2_1016.png", #"2_1017.png",#"2_1018.png", #"2_1019.png", #"2_1020.png", #"2_1021.png",#"2_1022.png", #"2_1023.png", #"2_1024.png", #"2_1025.png",#"2_1026.png", #"2_1027.png", #"2_1028.png",
#"2_1029.png",#"2_1030.png",#"2_1030.png",#"2_1029.png",
#"2_1028.png",#"2_1027.png",#"2_1026.png",#"2_1025.png",
#"2_1024.png",#"2_1023.png",#"2_1022.png",#"2_1021.png",
#"2_1020.png",#"2_1019.png",#"2_1018.png",#"2_1017.png",
#"2_1016.png",#"2_1015.png",#"2_1014.png"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
// Normal Animation
animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
animationImageView.animationImages = images;
animationImageView.animationRepeatCount=0;
animationImageView.animationDuration = 1;
[self.view addSubview:animationImageView];
[animationImageView startAnimating];
Good day! I have a question,I have this code for a Single Page application
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint endPosition = [touch locationInView:self.view];
if (self.imageStatus>0)
{
if (self.startPosition.x < endPosition.x)
{
// Right swipe
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:myViewController.view];
self.imageStatus --;
if (self.imageStatus == 0)
{
self.myImage.image = [UIImage imageNamed:#"blue_back.png"];
}
else
{
self.myImage.image = [UIImage imageNamed:#"black_back.png"];
}
}
}
if (self.imageStatus<2)
{
if (self.startPosition.x > endPosition.x)
{
// Left swipe
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:myViewController.view];
self.imageStatus ++;
if (self.imageStatus == 2)
{
self.myImage.image = [UIImage imageNamed:#"red_back.png"];
}
else
{
self.myImage.image = [UIImage imageNamed:#"black_back.png"];
}
}
}
when i open the app the default background is black, when i swipe at right it shows the blue_back.png or the blue background, when i swipe back it shows the default background which is black, and when its on black again and i will swipe it left it shows the red_back.png or the red background and vise versa. now what i want is, i will add another page with yellow background next to red, so when im in the default black page, when i swipe to left two times it shows the yellow. and also the swiping function, when i swipe it it takes maybe a second to switch a page, how can i implement the "Realtime Swiping" like in the table view, but horizontal, so it changes page fast. thanks!
Updated answer:
What you want is a paging scrollview. The trick is to add the views are the correct positions in the scrollView and set the scrollviews contentSize correctly. Example with two views:
- (void)viewDidLoad
{
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeBottom;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.bounds.size.height);
[self.view addSubview:scrollView];
UIView *redView = [[UIView alloc] init];
redView.frame = self.view.bounds;
redView.backgroundColor = [UIColor redColor];
[scrollView addSubview:redView];
UIView *blueView = [[UIView alloc] init];
CGRect frame = self.view.bounds;
frame.origin.x = 320;
blueView.frame = frame;
blueView.backgroundColor = [UIColor blueColor];
[scrollView addSubview:blueView];
}
Old answer:
I would be easier to make use of the build in gesture recognizers. Specifically UIPanGestureRecognizer.
Example:
In viewDidLoad:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(myAction:)];
[self.view addGestureRecognizer:pan];
And the action:
- (void) myAction:(UIPanGestureRecognizer*)gestureRec
{
if (gestureRec.state == UIGestureRecognizerStateEnded) {
NSLog(#"ended");
NSLog(#"%#", NSStringFromCGPoint([gestureRec velocityInView:self.view]));
}
}
The state determines when its ended as you want. And you can use the velocityInView to determine right/left and up/down movement
I have a UIViewController that i edit using IB. I put a UINavigationBar and a UISegmentedControl on the top and 3 UIViews under them. I want to be able to switch between the UIViews using an animation, but i only want to animate the UIViews, i want the navigationBar and athe segmentedControl to not move. I show the code how i do it now.
Any idea how i could only move the 3 views?
- (IBAction)segmentedControlValueChanged:(id)sender {
UISegmentedControl* segmentedControl = sender;
if(lastSelectedViewIndex != [segmentedControl selectedSegmentIndex]) {
CATransition *transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
if(lastSelectedViewIndex < [segmentedControl selectedSegmentIndex])
transition.subtype = kCATransitionFromLeft;
else
transition.subtype = kCATransitionFromRight;
transition.removedOnCompletion = YES; // force removal of animation when completed.
{
switch ([segmentedControl selectedSegmentIndex]) {
case 0:
[self.usageScenarioView setHidden:NO];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:0];
break;
case 1:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:NO];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:1];
break;
case 2:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:NO];
[self.pageControl setCurrentPage:2];
break;
}
}
lastSelectedViewIndex = [segmentedControl selectedSegmentIndex];
[self.view.layer addAnimation:transition forKey:nil];
}
}
Say your 3 views are named as view1, view2, view3. If you want to remove view1 and show view2 or view3, just do the existing code, but change
[self.view.layer addAnimation:transition forKey:nil];
into
[view1.layer addAnimation:transition forKey:nil];
that will animate the view1 only not the whole view. Similarly you can try,
[view2.layer addAnimation:transition forKey:nil];
[view3.layer addAnimation:transition forKey:nil];
more precisely, do like
transition.removedOnCompletion = YES; // force removal of animation when completed.
{
switch ([segmentedControl selectedSegmentIndex]) {
case 0:
[self.usageScenarioView setHidden:NO];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:0];
[self.usageScenarioView.layer addAnimation:transition forKey:nil];
break;
case 1:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:NO];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:1];
[self.loginCredentialsView.layer addAnimation:transition forKey:nil];
break;
case 2:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:NO];
[self.pageControl setCurrentPage:2];
[self.whatItCoversView.layer addAnimation:transition forKey:nil];
break;
}
}
lastSelectedViewIndex = [segmentedControl selectedSegmentIndex];
}
you can define a UIView* containerView in IB below your UISegmentedControl* segmentedControl which has the coordinates and position of the views you want.
Then you can inside that view you can do transition between the three UIViews you want with this function:
-(void) replacePreviousViewInContainerViewWith:(UIView*) newView {
[UIView transitionWithView:_containerView duration:0.7
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseIn
animations:^ {
[_containerView.subviews[0] removeFromSuperview];
[_containerView addSubview:newViewVC.view];
}
completion:^(BOOL finished) {
if (finished) {
NSLog(#"Now displaying %#.", [newView class]);
}
}];
[UIView commitAnimations];
}
and you call this function like this from where you want to change the Views:
UIViewController *newViewVC = [[UIViewController alloc] initWithNibName#"YOURNAME"];
newViewVC.view.frame = _containerView.frame;
[self replacePreviousViewInContainerViewWith: newViewVC.view];
How to implement SplitViewController on second level.
Actually what i want is to launch app with a login page and after login. I need SplitViewController.
This is how I do it. By removing the first viewContorller from the window and replacing it with the splitView
splitViewController = [[SplitViewController alloc]init];
// remove the current view and replace with splitViewController
[theWindow addSubview:splitViewController.view];
// Transition handling
NSString *subtypeDirection;
switch ([[UIApplication sharedApplication] statusBarOrientation]) {
case UIDeviceOrientationPortrait:subtypeDirection = kCATransitionFromRight;break;
case UIDeviceOrientationPortraitUpsideDown:subtypeDirection = kCATransitionFromLeft;break;
case UIDeviceOrientationLandscapeLeft:subtypeDirection = kCATransitionFromTop;break;
case UIDeviceOrientationLandscapeRight:subtypeDirection = kCATransitionFromBottom;break;
default: NSLog(#"break at subType direction");break;
}
CATransition *animation = [CATransition animation];
[animation setDuration:.5];
[animation setType:kCATransitionPush];
[animation setSubtype:subtypeDirection];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToSplitView"];
[self.navigationController.view removeFromSuperview];
Most of the lines here deals with transition and handling rotation.
self refers to the first ViewController whereas theWindow refers to application window. You can get to it by:[self superView];
For the same login -> splitview controller I'm doing the following:
a. Subclass UIStoryboardSegue and override perform:
#implementation SSPushSegue
- (void)perform
{
UIWindow* window = [self.sourceViewController view].window;
// Transition handling
NSString *subtypeDirection = kCATransitionFromRight;
switch ([UIApplication sharedApplication].statusBarOrientation)
{
case UIDeviceOrientationPortraitUpsideDown: subtypeDirection = kCATransitionFromLeft; break;
case UIDeviceOrientationLandscapeLeft: subtypeDirection = kCATransitionFromTop; break;
case UIDeviceOrientationLandscapeRight: subtypeDirection = kCATransitionFromBottom; break;
default: break;
}
CATransition *animation = [CATransition animation];
animation.duration = .5;
animation.type = kCATransitionPush;
animation.subtype = subtypeDirection;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[window.layer addAnimation:animation forKey:NSStringFromClass([self class])];
window.rootViewController = self.destinationViewController;
}
#end
b. Add "Custom Segue" from Initial View Controller to Destination and put your subclass name in the property field.