How can we move a view vertically upward and downwards? - iphone

I have a tableview in my main view, and I need to move this tableview vertically with touch.
I want to drag this tableview upwards and downwards when touching at any position, and when my finger moves upward or downward the tableview will move according to the touch.
I have lots of controllers in my main view, so i want to touch the tableview only.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
How do I use these delegates in the question above?
Thanks in advance.

YOu just make a class , inherited from UITableview
Make a class Drag
Drag.h should be like this
#interface Drag : UITableView {
CGPoint touchingPoint;
}
and Modify Drag.m File by including following methods
Drag.m
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
touchingPoint = pt;
[[self superview] bringSubviewToFront:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint pt = [[touches anyObject] locationInView:self];
[UITableView beginAnimations:nil context:nil];
[UITableView setAnimationDuration:0.1];
CGRect newFrame = self.frame;
newFrame.origin.y += pt.y - touchingPoint.y;
[self setFrame:newFrame];
[UITableView commitAnimations];
}
That's it.
Moreover , in Your viewDidLoad method,
put these lines
Drag *objDrag = [[Drag alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
objDrag.delegate = self;
objDrag.dataSource = self;
[objDrag setUserInteractionEnabled:YES];
[self.view addSubview:objDrag];
Remember: for dragging vertically , you must touch the separators of table otherwise it will consider touch to cell and will scroll instead of dragging.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event can be used to create table with tapping the view and getting the CGPoint of the view and after then setting the tableview frame with these coordinates.
Note:- It will be better to represent in User Interface if table drag is done with
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; with this function.

Related

Dismiss UIScrollView based on touch coordinates on UIImageView embedded into the UIScrollView

There is a UITableView with many photos. As a photo is tapped, a modalView is presented with UIScrollView, which further has an UIImageView embedded into it. The tapped image is presented here (similar to the Facebook app).
Now, i can move the image vertically using touchesMoved and CGRectMake.
What i want to achieve is that when i drag the image vertically, on reaching a certain threshold, the ImageView & ScrollView should disappear and we should be back to the tableView.
Image moved using:
- (void) touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(#"%f", pos.y);
if (pos.y>50&&pos.y<430){
[UIView setAnimationDelay:0];
[UIView animateWithDuration:0.4
animations:^{_photoImageView.frame=CGRectMake(0,pos.y-100,320,200);}
completion:^(BOOL finished){ }];
}
}
The action to return control to the tableView is needed here:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
if (pos.y>50&&pos.y<430){
[UIView setAnimationDelay:0];
[UIView animateWithDuration:0.4
animations:^{_photoImageView.frame=CGRectMake(0,140,320,200);}
completion:^(BOOL finished){ }];
}
else if(pos.y<50||pos.y>430)
//Code needed here !!
;
}
Further, if transparency can be achieved on the imageView background (i.e. Scrollview) such that the table view is visible with certain Alpha, that would be highly appreciated.
Dismiss with dismissModalViewControllerAnimated: (should use the newer method dismissViewControllerAnimated:completion:).
(This was assuming you presented with presentModalViewController:animated:, just adding a view to the screen is not a modal presentation). If you didn't really present a modal then just use removeFromSuperview.
When presenting a view modally the view below will be removed from display (it's expected not to be visible), so if you make the modal view transparent you will just see black below it. If you want to make the overlay view transparent you should just make it a subview, set the opacity (alpha) and use bringSubviewToFront:.

Get event on UIslider track

How to get an event on UISlider track? I am able to get the events on UISlider's button but not on track. How should I go about it?
Thanks
For doing this you need to subclass the UISlider and implement the touches event like: touchesBegan, touchesEnd,touchesCancelled,touchesMoved etc.
#interface yourSlider:UISlider
#end
#implementation yourSlider
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
#end
Subclass the UISlider and override its touchesBegan:withEvent: method. Get the point value from the touch event and calculate it's percentage by the point's .x value relative to the width of the slider.
Answers are correct but there is a workaround here , you can simply add a clear button equal to slider coordinations on your slider and detect the touch point on it then convert the X position to a slider value.
- (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
{
UIView *button = (UIView *)sender;
UITouch *touch = [[event touchesForView:button] anyObject];
CGPoint location = [touch locationInView:button];
NSLog(#"Location in button: %f, %f", location.x, location.y); \\ use this x to determine slider's value
}
Another workaround :
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(sliderTapped:)];
gr.delegate=self;
[Slider addGestureRecognizer:gr];
-(void)sliderTapped:(UIGestureRecognizer*)g{
UISlider* s = (UISlider*)g.view;
if (s.highlighted)
return;
CGPoint pt = [g locationInView: s];
CGFloat value = s.minimumValue + pt.x / s.bounds.size.width * (s.maximumValue - s.minimumValue);
[s setValue:value animated:YES];
}

touchesMoved reaching out of my view bounds

I have subclassed UIView and there initially my view will be in a default color and i need to fill some different color on touch (from x axis = 0 to user touched point),here the problem is touchesMoved even if i drag out of my self view bounds it is getting those points,how to restrict it to only for my self view bounds.
I googled & tried below snippets but of no luck
if([self pointInside:point withEvent:nil]){
[self fillColor];
}
My touchesMoved method is as below,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
endPoint = point;
NSLog(#"moved x: %f,y: %f",point.x,point.y);
if(CGRectContainsPoint([self frame], endPoint)){ // this also not working
[self fillColor];
}
}
Any help is appreciated in advance.
just set tag in viewDidLoad: method and use bellow logic..
fillColorView.tag = 111;
and use bellow logic in touchesMoved: method like bellow..
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *tap = [touches anyObject];
CGPoint pointToMove = [tap locationInView:fillColorView];
if([tap.view isKindOfClass:[UIView class]])
{
UIView *tempView=(UIView *) tap.view;
if (tempView.tag == 111){
[self fillColor];
}
}
}
hope this help you...
In your touchesMoved method, CGPoint point = [touch locationInView:self]; replcae self by the view in which you wants the touch to be worked.
self will get the complete view, you should pass your drawingView at there, so that it will detetc touch only on that view.

How to manage more than one UIImageView in iPhone

I wanna to create dynamically UIImageView on every tap on main view and also wanna to move all the created UIImageView by finger movement.I've got success to dynamic creation on every touch my code is below :
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self.view];
imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(pt.x,pt.y, 100, 100)] autorelease];
imgFilepath = [[NSBundle mainBundle] pathForResource:#"img" ofType:#"png"];
img = [[UIImage alloc] initWithContentsOfFile:imgFilepath];
imgView.tag=i;
[imgView setImage:img];
[img release];
[self.view addSubview:imgView];
}
Now I wanna to move any of the dynamically created UIImageView by finger movement on it.
Thanks.
Set the userInteractionEnabled property of the UIImageView to yes in the method you create it (i. e. the method you posted):
imgView.userInteractionEnabled = YES;
then you will most probably need to subclass UIImageView and implement the following methods to move your image view around:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
Hint: in the touchesMoved: etc. method, you'll want to track where the user's finger moved -- after reading some more docs you'll most probably end up using -[UITouch locationInView:] -- you'll need to convert the coordinates to the superview of your image view and then move the image view itself in this case.
In touchesBegan detect the imageView you have touched. In touchesMoved move the selected imageView by setting it's center to the touch point moved.

Issues in handling touches on subviews(uiviews) in UIScrollView

I have craeted a UIScrollView using code and i have created subviews which i am creating dynamically from the DB that are added to the array of views and to the UIScrollviews.I am using touchesBegan and touchesMoved methods.The subview which i am selecting is not recognized,but the control comes into touchesBegan and touches moved method.I have posted the code below.Please anybody help me to overcome from this issue.
I have created the scrollview through code and i have subviews in an array named "arrayofviews".these subviews are the same views which are in the array.I am able to move these subviews which are from the DB on a normal view,but when i added these views to the scrollview its not working.
I have tried so many solutions which are in the net,but i couldn't get a proper solution.
.m file
- (void)viewDidAppear:(BOOL)animated
{
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 374)];
scrollview.backgroundColor=[UIColor clearColor];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(300, 600)];
scrollview.showsVerticalScrollIndicator = YES;
scrollview.delaysContentTouches=NO;
.
.
.
.
//here i am retrieving the controls from the DB and adding into the "arrayofviews" array which is an NSMutableArray
//I have added subviews in this part to the scroll like
[scrollview addSubview:vw1];
[scrollview addSubview:vw2];
.
.
.
scrollview.userInteractionEnabled=NO;
scrollview.scrollEnabled=FALSE;
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
scrollview.userInteractionEnabled=YES;
[self.nextResponder touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation;
touchLocation.x=scrollview.contentOffset.x;
touchLocation.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
vw.userInteractionEnabled=YES;
if([touch view]==vw)
{
vw.center=touchLocation;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event //method to intiate the touch events
{
[self.nextResponder touchesMoved:touches withEvent:event];
UITouch *touch = [[event touchesForView:scrollview] anyObject];
CGPoint touchLocation1;
touchLocation1.x=scrollview.contentOffset.x;
touchLocation1.y=scrollview.contentOffset.y;
for(UIView *vw in arrayOfViews)
{
if([touch view]==vw)
{
vw.center=touchLocation1; //statement to move the control
}
}
}
change the line
scrollview.userInteractionEnabled=NO;
to
scrollview.userInteractionEnabled=YES;
reason
userInteractionEnabled tells the device, whether to accept touch events. As you disabled the touch on scrollView then how it could recived touches events...
cheers!!!