Hello Guys, am Manish, here to give a brief introduction to build a simple iOS app containing a sliding dock, and some image manipulating features. Here we are going to learn:
• Managing Views
• UIGestureRecognizer
a. Swipe Gesture (left and right)
b. Tap gesture
Let us first start from the basic. Open your Xcode (am showing Xcode 5.0).
Create a new project.
Name your application and select target device as iphone
Select your directory to save your project
Finally you reach this default home page for your application. See illustration 5
First open your storyboard from left pane. (the default name is MainStoryboard.storyboard)
As We are going to make one image editing application, we need to have one image view and you can do that by simply drag and drop from storyboard. Set its position as shown in the illustration 6. Drag another view and set it as shown to its left, this is going to be our sliding dock.
Drag and drop some UIButtons on the left sliding view. Also add an image for your dock (in this case i have added one translucent dock drawer image). I have introduced some other buttons below to show camera functionality, in next tutorial.
See illustration 7 below
Lets now add some UIGestures, add two swipe gestures on the image view below and one on sliding dock. Set direction of one of the swipe gestures to ‘right’,and the other to left,and the one for sliding dock to left. as shown in illustration. Now we are ready with the storyboard.
Lets begin some simple coding:
@property (weak, nonatomic) IBOutlet UIView *slidingDockView; @property (weak, nonatomic) IBOutlet UIImageView *imageView;
//image to be manipulated @property (weak, nonatomic) UIImage *image; @property (weak, nonatomic) UIImage *temporaryImage;
-(void)viewDidAppear:(BOOL)animated { image = [UIImage imageNamed:@"bg1.jpg"]; // setting a default image in background imageView.image = image; slidingDockView.frame = CGRectMake(-85, 0, 100, 410); }
Add viewDidAppear manually. this method gets called just after the viewDidLoad method.
slidingDockView.frame = CGRectMake(-85, 0, 100, 410); code sets the frame for your sliding dock in extreme left as shown in illustration. This allows only the drawer part of the dock to screen.
See illustration 8
- (IBAction)SwipeToAppearDock:(id)sender { [UIView beginAnimations:nil context:NULL]; slidingDockView.frame = CGRectMake(0, 0, 100, 410); [UIView commitAnimations]; NSLog(@"swipe R8"); }
This would animate the frame inside the view, on your swipe right gesture. The time duration can be set (i have set it as 0.3s). However this wont work until you check ‘ user interaction enabled’ . This allows image to rrecognise any gesture on it. This can be easily done through storyboard.
Go to storyboard> click on the background image> check identity inspector
see illustration 9 alongside.
N.B. No gesture would be recognised until you enable this.
- (IBAction)swipeToDisappearDock:(id)sender { [UIView beginAnimations:nil context:NULL]; slidingDockView.frame = CGRectMake(-85, 0, 100, 410); [UIView setAnimationDuration:0.3]; [UIView commitAnimations]; NSLog(@"swipe left"); }
ctrl-drag and the other left swipe gesture on ‘swipeToDisappearDock’ IBAction method.
This makes both the left swipe to work on the same code. What happens here is, on left swipe gesture (be it on the background image or the sliding dock) would move back the dock to its initial position.
Your Sliding Dock is ready to use. Run the program in your simulator by ctrl-r and this is the result see illustration 10