UIScrollView click coordinates - uiscrollview

I move a hidden uilabel to the tap point in a uiscrollview and then unhide it - works great. However when you pinch the uiscrollview to zoom in and tap again the label shows up in the wrong position. How can I scale the touchpoint by the scale of the uiscrollview to position it correctly?
-me
-(void)handleLongPress:(UILongPressGestureRecognizer*)sender {
CGPoint longTapPoint = [sender locationInView:self.view];
NSLog(#"LongTapPoint.x %f,LongTapPoint.y %f",longTapPoint.x,longTapPoint.y);
uil_tapBldgLabel.center = CGPointMake(longTapPoint.x, longTapPoint.y);
}

You may want to pass these touch events to the nextResponder so you do not get caught up with the scaling nuances.

Related

When I use a UIScrollView to zoom out an UIImageView, can I get the zoomed ImageView's position?

I init a scrollView's frame according to my imageView's frame, so does its contentSize. Then I set my imageView as his child in order to zoom out and it works well. But, The method below can only just get the (UIView *)view 's relative location to his father(scrollView).Actually, view.frame.origin = (0, 0), which is weird to me. How explain this situation? Can I get the zoomed imageView's absolute position(to rootview) ?
(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
Parameter view
What changes when UIScrollView is scrolling is its contentOffset & bounds not its subviews' frames.

Xcode How do you make circular hitboxes on buttons?

I'm using Xcode 5 to make an UIButton. I made the button in storyboard and set the background image to a .png of a circle with transparency. I set up a simple action for the button to display how many times it was pressed in a label.
When I press the corners of the circle on the screen, it still adds to the score. So the button is still keeping its square hitbox, even though it has a round image as its background. I searched everywhere for a way to make a circular hitbox, but I can't find anything. Is this even possible? Is there an alternative way to do this?
The button responds to touch on corners because the bounding box of the button is a rectangle no matter what image you set for background.
To achieve a circular UIButton you must make a circular bounding box by simply using this code:
- (UIView *)setRoundedView:(UIView *)roundedView toDiameter:(float)newSize {
CGPoint saveCenter = roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
roundedView.frame = newFrame;
roundedView.layer.cornerRadius = newSize / 2.0;
roundedView.center = saveCenter;
roundedView.layer.masksToBounds = YES;
return roundedView;
}
In this method you can pass any UIView or subclass of UIView in your case UIButton. So in code you must create an IBOutlet of you button and in your -viewDidLoad method simply put this line of code:
self.btnMyButton = (UIButton *)[self setRoundedView:self.profilePic toDiameter:34];
Assuming you UIButton outlet is btnMyButton and set the diameter as per your requirement.
Hope this helps.
Don't forget to import QuartzCore Framework.

NSScrollView position is always bottom [duplicate]

I am developing Mac app. In this app, I am using a NSScrollView's instance as scrollView.
And I have set a customView which is an instance of NSView in this ScrollView as:
[scrollView setDocumentView:customView];
But, the NSScrollView's vertical Slider always points to the bottom of the view as:
I want that the slider always points to the top of the custom View as:
How can I make this change?
Plz help.
I have solved my problem by setting the scrollToPoint property of ScrollView's ContentView.
Here is the code:
[[scrollView verticalScroller] setFloatValue:0.0];
[[scrollView contentView] scrollToPoint:NSMakePoint(0.0, y)];
// here y = (difference b/w scrollView's content size height and scrollView's height)

Draw NSView background partially, with a gradient

I have a NSView, subclassed, with custom background drawing, filling it with a gradient.
In IB, I've put a checkbox on it, somewhere in the middle.
This is the drawRect method.
-(void) drawRect:(NSRect)dirtyRect {
CGFloat sc = 0.9f;
CGFloat ec = 0.6f;
NSColor* startingColor = [NSColor colorWithDeviceRed:sc green:sc blue:sc alpha:1];
NSColor* endingColor = [NSColor colorWithDeviceRed:ec green:ec blue:ec alpha:1];
NSGradient *grad = [[NSGradient alloc] initWithStartingColor:startingColor endingColor:endingColor];
[grad drawInRect:dirtyRect angle:270];
}
What happens is, this same method gets called to draw the whole view area first and then for the part, where NSButton (checkbox) lies on top of it. OF course the checkbox background is drawn with a complete gradient and it is not right, since the portion is much smaller. The same happens with other controls I put on the said NSView.
What is the suggested approach on such thing?
One option is to make controls height the same as the views' but this will result in problems in the future.
The answer is, always draw the WHOLE area of the view, not just the dirtyRect
[grad drawInRect:[self bounds] angle:270];

In NSScrollView the vertical slider always points to bottom of the view

I am developing Mac app. In this app, I am using a NSScrollView's instance as scrollView.
And I have set a customView which is an instance of NSView in this ScrollView as:
[scrollView setDocumentView:customView];
But, the NSScrollView's vertical Slider always points to the bottom of the view as:
I want that the slider always points to the top of the custom View as:
How can I make this change?
Plz help.
I have solved my problem by setting the scrollToPoint property of ScrollView's ContentView.
Here is the code:
[[scrollView verticalScroller] setFloatValue:0.0];
[[scrollView contentView] scrollToPoint:NSMakePoint(0.0, y)];
// here y = (difference b/w scrollView's content size height and scrollView's height)

Resources