Popover from a CGPoint - xcode

How to display a prepared popover (TableView inside) with the arrow on a specific point? Basically, I have a CGPoint screenPointContainer and I want to show my popover there. No rectangles, no buttons.
Just a single point.
detailsPop.popoverContentSize = CGSizeMake(250, 300);
CGRect myrect = CGRectMake(screenPointContainer.x, screenPointContainer.y, ??, ??);
[detailsPop presentPopoverFromRect:myrect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

Can you not just create a CGRect with a 1 pixel size? Which would basically be a point:
CGRect myRect = CGRectMake(screenPointContainer.x, screenPointContainer.y, 1, 1);

I didn't find the reason of that coordinate distortion (60 units upper) and had to manually edit the coordinates, including orientation checks. Rectangle's H and W are both 0.
Now it is OK:
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
CGRect myrect2 = CGRectMake(screenPointContainer.x+320, screenPointContainer.y+60, 0, 0);
[detailsPop presentPopoverFromRect:myrect2 inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
CGRect myrect2 = CGRectMake(screenPointContainer.x, screenPointContainer.y+60, 0, 0);
[detailsPop presentPopoverFromRect:myrect2 inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

Related

how can I make a scrollView autoscroll?

I have this scrollView:
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.scrollView.contentSize = CGSizeMake(320,3000);
_scrollView.frame = CGRectMake(0, 45, 320, 420);
and I want to make it autoscroll very slowly downward to the end so that the user can see the content (as in movie credits), eventually with a button to stop/play, but to follow the user gestures when touching the interface.
How can I do this?
Thanks,
You can use:
[_scrollView setContentOffset:CGPointMake(x,y) animated:YES];
and use the x and y as the touch points on the screen you can capture.
You can also do an animation with CoreAnimation:
[UIScrollView beginAnimations:#"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:1.0f];
[scroll setContentOffset:CGPointMake(x, y)];
[UIScrollView commitAnimations];
this adapted code did the trick (source http://sugartin.info/2012/01/21/image-sliding-page-by-page-uiscrollview-auto-scrolling-like-image-slider/)
PS : each image is 280 by 200
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
}
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<=10; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"sti%02i.jpg",i]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, ((i-1)*scrMain.frame.size.height+100), 280, 200)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width, scrMain.frame.size.height*10)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(scrollingTimer) userInfo:nil repeats:YES];
}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset ( which page is being displayed )
CGFloat contentOffset = scrMain.contentOffset.y;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
// if page is not 10, display it
if( nextPage!=10 ) {
[scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=nextPage;
// else start sliding form 1 :)
} else {
[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=0;
}
}
You can set x if you want to scroll horizontally, otherwise set y to scroll vertical.
[_scrollView setContentOffset:CGPointMake(x, y) animated:YES];
and modify the co-ordinates accordingly.

How to calculate correct coordinates for selected text in NSTextView?

I need to highlight line with caret in NSTextView using CALayer overlay and grab rect for line with this code in my subclassed NSTextView...:
- (NSRect)overlayRectForRange:(NSRange)aRange
{
NSScreen *currentScreen = [NSScreen currentScreenForMouseLocation];
NSRect rect = [self firstRectForCharacterRange:aRange];
rect = [self convertRectToLayer:rect];
rect.origin = [currentScreen flipPoint:rect.origin];
rect = [self.window convertRectToScreen:rect];
return rect;
}
...and put overlay:
- (void)focusOnLine
{
NSInteger caretLocation = [aTextView selectedRange].location;
NSRange neededRange;
(void)[layoutMgr lineFragmentRectForGlyphAtIndex:caretLocation effectiveRange:&neededRange];
CALayer *aLayer = [CALayer layer];
[aLayer setFrame:NSRectToCGRect([aTextView overlayRectForRange:neededRange])];
[aLayer setBackgroundColor:[[NSColor redColor] coreGraphicsColorWithAlfa:0.5]];
[[aTextView layer] addSublayer:aLayer];
}
As a result, the selection overlay coincides with width of desired line, but absolutely not matches by Y axis (X axis is ok).
What am I missing?
Okay, I completely rewrote method overlayRectForRange: and now all works fine. There is fixed code:
- (NSRect)overlayRectForRange:(NSRange)aRange
{
NSRange activeRange = [[self layoutManager] glyphRangeForCharacterRange:aRange actualCharacterRange:NULL];
NSRect neededRect = [[self layoutManager] boundingRectForGlyphRange:activeRange inTextContainer:[self textContainer]];
NSPoint containerOrigin = [self textContainerOrigin];
neededRect.origin.x += containerOrigin.x;
neededRect.origin.y += containerOrigin.y;
neededRect = [self convertRectToLayer:neededRect];
return neededRect;
}

ipad app - most views rotate ok. One view does not

I am building an iPad app where all views should autorotate. One view is giving me a headache.
I know that variants of this question have been asked a couple of times before, but I cannot find a working solution..
I flip to this view by calling a routine in the app. delegate. The code for this routine is:
- (void)flipToBack {
//NSLog(#"%s", __FUNCTION__);
DrawingViewController *drawView = [[DrawingViewController alloc] initWithNibName:#"Draw" bundle:nil];
[self setDrawingViewController:drawView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:window cache:YES];
[self.window addSubview:[drawView view]];
[UIView commitAnimations];
// NSLog (#" FINISHED ");
}
This is a view controller (mvc) with a UIView subview, the latter, where drawing takes place. Neither the mvc nor the subview are rotating properly.
The code I'm using to invoke the rotation in the mvc is:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
The mvc has two tool bars, top and bottom, and these need to resize. These should also stay top and bottom regardless of the rotation.
The project summary shows all orientations are supported, but I think that this is just for app. initialization.
I have all struts on for the mvc.
I'm building the toolbars like this:
UIInterfaceOrientation fieldOrient = [[UIApplication sharedApplication] statusBarOrientation];
if ((fieldOrient == UIDeviceOrientationLandscapeLeft) || (fieldOrient == UIDeviceOrientationLandscapeRight)) {
CGRect frame = CGRectMake(0, 50, 1024, 35 );
topToolbar.frame = frame;
} else {
CGRect frame = CGRectMake(0, 50, 768, 35 );
topToolbar.frame = frame;
UIInterfaceOrientation fieldOrient = [[UIApplication sharedApplication] statusBarOrientation];
if ((fieldOrient == UIDeviceOrientationLandscapeLeft) || (fieldOrient == UIDeviceOrientationLandscapeRight)) {
CGRect frame = CGRectMake(0, 650, 1024, 35 );
bottomToolbar.frame = frame;
} else {
CGRect frame = CGRectMake(0, 950, 768, 35 );
bottomToolbar.frame = frame;
I guess I'm missing something obvious.. Any help/pointers would be appreciated.
Try setting the frame in the will rotate to interface orientation method:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ((toInterfaceOrientation == UIDeviceOrientationLandscapeLeft) || (toInterfaceOrientation == UIDeviceOrientationLandscapeRight)) {
CGRect frame = CGRectMake(0, 650, 1024, 35 );
bottomToolbar.frame = frame;
} else {
CGRect frame = CGRectMake(0, 950, 768, 35 );
bottomToolbar.frame = frame;
}
The answer for me was to present the view as a modal view. It's not ideal but it works.

touch draw when rotating

I'm working on a touch draw app, everything is working fine in portrait, but when i rotate my iPad the line i draw when touching is 5 cm above my finger.
iv been googling a lot to try and find out how x & y changes when rotating.
can any of you help me ?
my code for drawing it this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= -20;
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSaveGState(UIGraphicsGetCurrentContext());
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), f5);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), f1, f2, f3, f4);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;}}
I was using bounds in my appDelegate and frame in my draw code ...
i changed it to:
UIGraphicsBeginImageContext(self.view.bounds.size);
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.bounds.size.width,self.view.bounds.size.height)];

Image size resets to original after performing pich using UIPichGestureRecognizer in iphone

I have one view controller in which I have added UIScrollView & UIImageView programatically. I have added UIPichGestureRecognizer to the UIImageView. My UIImageView is added to UIScrollView as a subview.
My problem is when I try to pinch the image , it zoom in. But when I release the touches from screen it again come to its default size. I can not find the error in code. Please help me.
Below is my code
- (void)createUserInterface {
scrollViewForImage = [[UIScrollView alloc]initWithFrame:CGRectMake(20.0f, 60.0f, 280.0f, 200.0f)];
scrollViewForImage.userInteractionEnabled = YES;
scrollViewForImage.multipleTouchEnabled = YES;
scrollViewForImage.backgroundColor = [UIColor redColor];
scrollViewForImage.autoresizesSubviews = YES;
scrollViewForImage.maximumZoomScale = 1;
scrollViewForImage.minimumZoomScale = .50;
scrollViewForImage.clipsToBounds = YES;
scrollViewForImage.delegate = self;
scrollViewForImage.bouncesZoom = YES;
scrollViewForImage.contentMode = UIViewContentModeScaleToFill;
[self.contentView addSubview:scrollViewForImage];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 200.0f)];
[imageView setBackgroundColor:[UIColor clearColor]];
imageView.userInteractionEnabled = YES;
imageView.multipleTouchEnabled = YES;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[pinchRecognizer setDelegate:self];
[imageView addGestureRecognizer:pinchRecognizer];
//[self.contentView addSubview:imageView];
[self.scrollViewForImage addSubview:imageView];
scrollViewForImage.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)inScroll {
return imageView;
}
-(void)pinch:(id)sender {
[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
From what I can see the problem lies here:
scrollViewForImage.maximumZoomScale = 1;
You are setting the maximum zoom scale of the image to 1 x its full size. This means once you finish pinching the image, it will scale back to 1 x its size.
If you want to be able to zoom the image to a larger size, try settings this value to be higher than 1. e.g.
scrollViewForImage.maximumZoomScale = 3;

Resources