I want to create a collapsing animated UISlider. Basicly, the slider will the size of the thumbimage until it is touched at which point it expands to full size while it is being changed. After the value is changed and the slider is let go, the slider will collapse back to the original size (the size of the thumbimage).
I tried working with touchesBegan and touchesEnd but this didn't get me very far.
So far i've subclassed UISlider and overrode the beginTrackingWithTouch and endTrackingWithTouch. This code kind of accomplishes the collapsing effect (Without the animation of course) but the thumb slider doesn't change anymore. Any ideas on how best to do this?
#import "CollapsingUISlider.h"
#implementation CollapsingUISlider
/*-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width * 4.0f, self.frame.size.height);
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width * 0.25f, self.frame.size.height);
[super touchesEnded:touches withEvent:event];
}*/
-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 400, self.frame.size.height);
return YES;
}
-(void)endTrackingWithTouch:(UITouch*)touch withEvent:(UIEvent *)event {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 20, self.frame.size.height);
}
-(BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 400, self.frame.size.height);
return self.tracking;
}
#end
I ended up doing something like this:
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[platformView addGestureRecognizer:panRecognizer];
-(void)move:(id)sender {
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if (platformView.center.y < platformCenter.y - offset) {
platformView.center = CGPointMake(platformCenter.x,platformCenter.y-offset);
((UIPanGestureRecognizer*)sender).enabled = NO;
} else if (platformView.center.y > platformCenter.y) {
platformView.center = CGPointMake(platformCenter.x,platformCenter.y);
((UIPanGestureRecognizer*)sender).enabled = NO;
}
}
Related
I'm developing a drawing app which lets user to draw a UIbezierpath on a UIView (InsideView) subviewed by it's superView (self.view). I'm using conventional drawing codes in the InsideView like touchesBegan:, touchesMoved:, touchesEnded:and I'm handling pinch zoom code handlePinch:(UIPinchGestureRecognizer *)recognizer: inside the viewController class (which is InsideView's superView).
When I draw first, I can do the pinch zoom after it but when I do the pinch zoom first, the drawing code (touchesBegan etc) doesn't get called after UIPinchGestureRecognizer is fired and it doesnt draw anything on InsideView. What am I doing wrong? Thanks in advance.
//Code in the InsideView (which is a UIView subclass and is a subview for self.view)
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
path = [UIBezierPath bezierPath];
[path setLineWidth:7.0];
pointsArray = [NSMutableArray array];
finish = NO;
}
return self;
}
-(void)drawRect:(CGRect)rect{
[[UIColor redColor] setStroke];
[path stroke];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path moveToPoint:p];
[pointsArray addObject:[NSValue valueWithCGPoint:p]];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self];
[path addLineToPoint:p];
[self setNeedsDisplay];
[pointsArray addObject:[NSValue valueWithCGPoint:p]];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[path closePath];
//Smoothing the path.
path.miterLimit=-10;
[self setNeedsDisplay];
finish = YES;
}
//Code in the viewController (InsideView's superView)
- (void)viewDidLoad {
[super viewDidLoad];
InsideView* sV = [InsideView new];
[sV setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:sV];
//Pinch
UIPinchGestureRecognizer*pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(handlePinch:)];
pinch.delegate =self;
[sV addGestureRecognizer:pinch];
}
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{
if ([recognizer numberOfTouches] < 2)
return;
if (recognizer.state == UIGestureRecognizerStateBegan) {
lastScale = 1.0;
lastPoint = [recognizer locationInView:self.view];
}
// Scale
CGFloat scale = 1.0 - (lastScale - recognizer.scale);
[sV.layer setAffineTransform:
CGAffineTransformScale([sV.layer affineTransform],
scale,
scale)];
lastScale = recognizer.scale;
// Translate
CGPoint point = [recognizer locationInView:self.view];
[sV.layer setAffineTransform:
CGAffineTransformTranslate([sV.layer affineTransform],
point.x - lastPoint.x,
point.y - lastPoint.y)];
lastPoint = [recognizer locationInView:self.view];
}
-(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{
sV.transform = CGAffineTransformScale(sV.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale = 1.0;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
I've developed a UIViewController where I used them together without any problem,s maybe a solution could be to move this logic from UIView to the UIViewController. So at viewDidLoad you can define the pinch recognizer like this
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:#selector(handlePinch:)];
[self.view addGestureRecognizer:pinch];
of course also the method handlePinch: should be moved to the UIViewController, together with touchesBegan:withEvent:,
touchesMoved:withEvent:, touchesEnded:withEvent: methods
what you should change in these methods is that you should change
self
to
self.yourSpecificCurrentUIView
I have to perform all the gesture on my imageview at a time, means user can move imageview on single tap at a time on double tap also zooming and rotation possible.Now i am found this methods to perform multiple gesture but unable to perform zoom and roation.
- (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
Before i am applied UIPinchGestureRecognizer,UIRotationGestureRecognizer and UIPanGestureRecognizer but it handle only one gesture at a time.Please help me to solve this or give me any other better option if possible.
UIPinchGestureRecognizer *pinchGesture = ... //initialize
pinchGesture.delegate = self;
//same for rotation gesture
//and then implement delegate method
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
Set the gesture like this.
self.view.multipleTouchEnabled=YES;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveViewWithGestureRecognizer:)];
panGestureRecognizer.delegate=self;
[self.sub_View addGestureRecognizer:panGestureRecognizer];
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotationWithGestureRecognizer:)];
rotationGestureRecognizer.delegate=self;
[self.sub_View addGestureRecognizer:rotationGestureRecognizer];
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinchWithGestureRecognizer:)];
pinchGestureRecognizer.delegate=self;
[self.sub_View addGestureRecognizer:pinchGestureRecognizer];
[self.imageView setUserInteractionEnabled:YES];
Handle the action
-(void)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer{
[self.superImage_View setAlpha:0.5];
if (pinchGestureRecognizer.state == UIGestureRecognizerStateEnded) {
[self.superImage_View setAlpha:1.0];
}
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
pinchGestureRecognizer.scale =1.0;
}
-(void)handleRotationWithGestureRecognizer:(UIRotationGestureRecognizer *)rotationGestureRecognizer{
[self.superImage_View setAlpha:0.5];
if (rotationGestureRecognizer.state == UIGestureRecognizerStateEnded) {
[self.superImage_View setAlpha:1.0];
}
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotationGestureRecognizer.rotation);
rotationGestureRecognizer.rotation = 0.0;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
-(void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer{
//CGPoint touchLocation = [panGestureRecognizer locationInView:self.sub_View];
// self.imageView.center = touchLocation;
[self.superImage_View setAlpha:0.5];
CGPoint translation = [panGestureRecognizer translationInView:self.view];
self.imageView.center = CGPointMake(self.imageView.center.x + translation.x,
self.imageView.center.y + translation.y);
[panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];
if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [panGestureRecognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
// NSLog(#"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(self.imageView.center.x + (velocity.x * slideFactor),
self.imageView.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.imageView.center = finalPoint;
} completion:nil];
[self.superImage_View setAlpha:1.0];
}
}
I have a problem, I want to drag a label with finger, and I can't even register a touch on it. What can be a problem? self is my viewcontroller and self.label is my label.
EDIT:
My label is made programmatically over an uiimageview. UserInteractionEnables is set to YES.
I am not sure what is wrong, here is code in its entirety:
Init:
UIImage *myImage = [UIImage imageNamed:#"fish.jpg"];
UIImageView *mainImageView = [[UIImageView alloc] initWithImage:myImage];
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view = mainImageView;
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
self.label.userInteractionEnabled = YES;
self.label.text = #"Hello, World";
[self.view addSubview:self.label];
Dragging:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([[touch view] isEqual:self.label])
{
NSLog(#"touch on label");
self.startLocation = [[touches anyObject] locationInView:self.label];
// startLocation is a CGPoint declare globaly in .h..and lblName is your UILabel
}
}
- (void) touchesMoved:(NSSet *)touches withEvent: (UIEvent *)event
{
UITouch *touch = [[event allTouches]anyObject];
if([touch view] == self.label)
{
CGPoint pt = [[touches anyObject] previousLocationInView:self.label];
CGFloat dx = pt.x - self.startLocation.x;
CGFloat dy = pt.y - self.startLocation.y;
CGPoint newCenter = CGPointMake(self.label.center.x + dx, self.label.center.y + dy);
self.label.center = newCenter;
}
}
EDIT 2:
I have also tried this code, but it doesn't work too. I think the problem is not in these methods but with registering touches.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.startLocation = [[touches anyObject] locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self.label];
self.label.center = CGPointMake(self.label.center.x + point.x - self.startLocation.x, self.label.center.y + point.y - self.startLocation.y);
}
Make sure you check "User Interaction Enabled" on the label in interface builder, or if you you doing it in code, userInteractionEnabled=YES
By default, UILAbel does not respond to touches. You need to enable the userInteractionEnabled property.
label.userInteractionEnabled = YES;
harlanhaskins wrote:
I have a view called userSlider programmaticaly added in viewDidLoad:
self.userSlider = [[UserSliderView alloc] initWithFrame:CGRectMake(0, -120, 320, 155)];
[self.userSlider setUser:user];
[self.userSlider configureView];
[self.view addSubview:self.userSlider];
And a tableview called gameTableView.
I originally had added the userSlider into the xib as a custom view (because UserSlider has its own xib), and when I implemented these touch methods:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = (UITouch*)[touches anyObject];
start = [touch locationInView:self.view].y;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(start < 0)
{
return;
}
UITouch *touch = (UITouch *)[touches anyObject];
CGFloat now = [touch locationInView:self.view].y;
CGFloat diff = now - start;
sliderDirectionIsUp = diff < 0; //sliderDirectionIsUp is a BOOL
if ((self.userSlider.frame.origin.y == 0) && ((now < 120) || (now > 155))) {
return;
}
float newCenterY = self.userSlider.center.y + diff;
if (newCenterY < roundf(self.userSlider.frame.size.height / 2)) {
self.userSlider.center = CGPointMake(self.userSlider.center.x, newCenterY);
CGRect tableViewFrame = self.gameTableView.frame;
tableViewFrame.origin.y += diff;
tableViewFrame.size.height -= diff;
[self.gameTableView setFrame:tableViewFrame];
}
start = now;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGRect tableViewFrame = self.gameTableView.frame;
if (sliderDirectionIsUp)
{
tableViewFrame.origin.y = 28;
tableViewFrame.size.height = (self.view.frame.size.height - tableViewFrame.origin.y);
//animate userSlider out of visible area
[UIView animateWithDuration:.3 animations:^{
self.userSlider.center = CGPointMake(self.userSlider.center.x, -roundf(self.userSlider.bounds.size.height/2.) + 35);
[self.gameTableView setFrame:tableViewFrame];
}];
}
else if(start >= 0)
{
tableViewFrame.origin.y = (self.userSlider.frame.size.height - 7);
tableViewFrame.size.height = (self.view.frame.size.height - tableViewFrame.origin.y);
//animate userSlider with top to mainviews top
[UIView animateWithDuration:.3 animations:^{
self.userSlider.center = CGPointMake(self.userSlider.center.x, roundf(self.userSlider.bounds.size.height/2.) - 0.5);
[self.gameTableView setFrame:tableViewFrame];
}];
}
}
then both views would move how I wanted them to.
But now when I add it programmatically, suddenly the tableView doesn't change unless I tap inside the area after moving it. It's really weird.
Any ideas?
If your userSlider`s frame is
CGRectMake(0, -120, 320, 155)
I doubt whether your userSlider can receive the sender event .Because if a UIControl`s frame be out of the superView , it may not receive the sender event(the outside part) . You can set the frame
CGRectMake(0, 0, 320, 155)
to have a try .
I have 3 UIViews: needToDragView, its superView, and self.view. When I use touchesMoved: to drag only my needToDragView(lowest in 3Views). But it can drag all 3View. How to detect the lowest UIView?
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UIView *dragView = [[touches anyObject] view];
CGPoint newCenter = [[touches anyObject] locationInView:dragView.superview];
dragView.center = newCenter;
}
Try this:
Make sure you set UserInteractionEnabled = YES; for needToDragView.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
{
UITouch *touch = [touches anyObject];
if ([touch view]== needToDragView) {
// Drag Image Here
}
else {
// NSLog("Wrong");
}
}
UPDATE
Instead of this, you can also use UIPanGestureRecognizer like this:
UIPanGestureRecognizer pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(function:)];
[pan setMinimumNumberOfTouches:1];
[pan setMaximumNumberOfTouches:2];
[needsToDragView addGestureRecognizer:pan];
[needsToDragView setUserInteractionEnabled:YES];
- (void)function:(UIPanGestureRecognizer *)recognizer
{
NSLog(#"Moving View");
mainView= recognizer.view;
CGPoint translatedPoint = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan)
{
firstX = [mainView center].x;
firstY = [mainView center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[mainView setCenter:translatedPoint];
}