How to Resize iOS UI component same as Interface Builder - iphone

I'm working on a iOS app where in a screen, user can have multiple UI component. Also, user can drag and reposition those component.
Now, I want all UI component to be resized by user through finger touch(same as we can resize in Interface Builder with mouse).
EDIT:
As above screen shot of IB, we can hold any tiny squares around button with mouse and resize the button.Same thing I need to implement in my App by finger gesture (probably by UIPanGestureRecognizer).

I recommend you that you add a pinch gesture in those view to user can make zoom.
UIPinchGestureRecognizer *twoFingerPinch =
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scaleObject:)] autorelease];
[[self view] addGestureRecognizer:zoomGesture];
and the method:
- (void)scaleObject:(UIPinchGestureRecognizer *)recognizer
{
NSLog(#"Object scale: %f", recognizer.scale);
}
and change the object size with that scale. Moreover you can use other gestures like:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer

If you reject solution with pinch recognizer. I would suggest creating custom wrapper for UI components, that would create little squares in corners of the frame and in the middle of every side. Every square should have UIPanGestureRecognizer to control size of the object.

Guys thanks to this Beautiful Example, I'm able to achieve what I wanted to.
Now I am able to put editing handles & resize any UI component easily.
After searching about a week I got this solution, so thought of sharing.

Related

UIGestureRecognizers for view in a scroll view

I'm using a few UIGestureRecognizers to pan, rotate and scale a view, which resides inside a scroll view.
My problem is that sometimes the scroll view eats the touches before the gesture recognizers do, so when this happens I end up zooming the scroll view instead of dragging the view. (It doesn't happen all the time. I still can't describe how to reproduce this behavior).
I'm pretty sure this can be solved in some way. For example MPMoviePlayerController doesn't have this problem: in fact, you can put it in a scroll view, and when you pinch it, it works just fine (i.e. it doesn't zoom the outer scroll view too). Does anyone know how does MPMoviePlayerController achieve this?
I've already searched for answers on SO, with no results. Thanks!
Disclaimer: This is just an idea, I haven't tested this.
UIGestureRecognizerDelegate defines gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:. You could try implementing this on your gesture recognizer delegate. This won't prevent the scroll view from zooming. To prevent zooming you could try temporary setting maximumZoomScale minimumZoomScale to zoomScale in the recognizer action method.
At a guess your rotation is conflicting with zoom - both probably require two touches?
In which case try creating a one touch rotation.
Or disable zooming on the scrollview and implement zooming/scaling via pinch gesture recognizer.
see:
http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/
Could you not set scrollEnabled(NO) on your UIScrollView?
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html
If the value of this property is YES , scrolling is enabled, and if it
is NO , scrolling is disabled. The default is YES.
When scrolling is disabled, the scroll view does not accept touch
events; it forwards them up the responder chain.
Things like zoomToRect:animated: don't say anything about being disabled by this flag; I'd assume that would keep working when you pan/zoom/scroll in response to your gesture events.
I think you can differentiate zoom in/zoom out and scrolling functionality by identify number of tap option on UIScrolView. Here i do same things as you want. you may try it.
- (void)viewDidLoad {
[super viewDidLoad];
imgview=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"2.png"]];
view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 400)];
view1.backgroundColor=[UIColor greenColor];
[view1 addSubview:imgview];
objscrollview.contentMode=UIViewContentModeScaleToFill;
objscrollview.contentSize=CGSizeMake(300, 400);
objscrollview.minimumZoomScale=1;
objscrollview.maximumZoomScale=10;
[objscrollview addSubview:view1];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap)];
singleFingerTap.numberOfTapsRequired = 1;
[objscrollview addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
}
//Single tap on scrollview call below method
-(void)handleSingleTap
{
NSLog(#"Singletap identify");
}
//While perform zoom in/zoom out action on scroll view it's delegate method call
//In this method, we are return view that want to zoom in/Zoom out..
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView1
{
NSLog(#"hi++++++++++++++++++");
return imgview;
}

iPhone: Slide photos by finger

I want to implement something like Iphone Photo Gallery... How to slide images by finger, and how to do that animation if I want to slide photos by clicking on button ? Thanks...
Have a look at Three20 project, it has a lot of this gallery stuff already implemented and is open source. It has swiping through images implemented. If you want to do it by yourself, UISwipeGestureRecognizer would be a good starting point. Have a look at the sample code associated with it.
You need to use a PanGestureRecogniser on the view you want to slide.
UIPanGestureRecognizer *imgpan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(scrollBarDrag:)];
[imgpan setMaximumNumberOfTouches:1];
[self.svContainer addGestureRecognizer:imgpan];
[imgpan release];
Define this & as the pan is happening, slide in the next image...Hope i was clear.

Drag UIView without disabling its child controls iPhone/iPad

I have a UIView control (white rectangle in image)
Moreover I am able to drag that control...
And when I press the button I load a subview which is another nib that I created and I placed random controls in it to illustrate my point...
If you guys are interested in finding out how I placed that nib file in that UIView control take a look at this question. I don't thing you have to read it in order to understand my question.
Anyways the problem when loading that nib file is that I can no longer drag the top UIView. Because of this I changed:
for:
in the UIView of the subview. In otherwords the UIView of the nib file that I am placing in the UIView that has the white background.
and when I did that I was able to drag the control but the controls inside the subview no longer works. I have also tried placing the touchesMoved method in the subview instead but when I do that the application behaves strange. Plus the purpose of placing the nib file in a UIView control was to avoid repeating the same drag funcionality on several nib files.
I actually need to create an application like a power point presentation and I need to change the slide as the user slides the UIView and if it's cords are less than x for example then I load the next slide (nib file) in that uiview controller. Maybe there is a simpler way of doing what I need but if I get this drag to work I am done cause I would just have to do that functionality just once.
You should leave the UserInteractionEnabled flag on for your subview if you want it to respond to events.
One way to achieve this would be to do your dragging using a UIGestureRecognizer.
UIPanGestureRecognizer is perfect for this (UIGestureRecognizer at apple)
Basically you'd attach the gesturerecognizer to the view which you want to pan then adjust it's position in the callbacks it provides.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture:)];
panGesture.minimumNumberOfTouches = 1;
[draggableSubview addGestureRecognizer:panGesture];
[panGesture release];
Then in the handlePanGesture method you figure out how far the user panned using the translationInView method of the recognizer which it gets passed and translate the subview accordingly.

How to implement the traditional imageView-inside-a-scrollView pan gesture with two fingers instead of one?

I have an imageView nested inside of a scrollView that allows me to view an image, pinch to zoom, and pan around if I am zoomed in enough. Using a custom GestureRecognizer, I have (per the request of the person I'm building this app for) overridden the default behavior of the one finger pan so that it does something other than pan. This works perfectly.
Now the problem is that I still need the ability to pan around the image like I could with the one finger pan, I just need this to now be with two fingers. Is there a solution that can be implemented to utilize the already available features of the nested imageView? Or do I need to go through and after the two finger gesture is recognized, write my own custom pan-logic?
Any thoughts here are greatly appreciated!
Thanks
With the newer versions if iOS you can merely adjust the parameters of the 'default' UIPanGenstureRecognizer that is already attached to the scrollView
for (UIGestureRecognizer *gestureObj in scrollView.gestureRecognizers) {
if ([gestureObj isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *panObj = (UIPanGestureRecognizer *) gestureObj;
panObj.minimumNumberOfTouches = 2;
panObj.maximumNumberOfTouches = 2;
}
}
That should 'shift' the behavior to the 2 finger level

gesture recognizer for whole UIView

I want to find a way to animate a whole UIView which contains some ui components within it. Here's my code snippet:
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotatePiece:)];
[piece addGestureRecognizer:rotationGesture];
[rotationGesture release];
the "piece" is a uiview here which I want to rotate and it works perfectly fine. But when I add some other UI components (like UIbutton) inside it (piece view), gestures are not recognized properly when user touches on those UI components. Essentially, I want the parent to listen to the gestures even when user touches the child object.
Any ideas?
you just need to addGestureRecognizer to your subviews you add to your view. As in the rotate piece method you might be rotating your original view only,so it will work fine.
You can add a transparent UIView to cover the whole area and add the gesture recognizer there.