setting view boundaries - objective-c

I have a scrollview with an image as a subview. I would like to set the boundaries of the scrollview to be the size of the image view, so that you wouldn't be able to see any of the background.
I don't want this happening anymore.
The weird part is, that after you zoom in or out on the image, then the boundaries seem to fix themselves, and you can no longer move the image out of the way and see the background.
This is what I have going for code:
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
// return which subview we want to zoom
return self.imageView;
}
-(void)viewDidLoad{
[super viewDidLoad];
[self sendLogMessage:#"Second View Controller Loaded"];
//sets the initial view to scale to fit the screen
self.imageView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
//sets the content size to be the size our our whole frame
self.scrollView.contentSize = self.imageView.image.size;
//setes the scrollview's delegate to itself
self.scrollView.delegate = self;
//sets the maximum zoom to 2.0, meaning that the picture can only become a maximum of twice as big
[self.scrollView setMaximumZoomScale : 2.5];
//sets the minimum zoom to 1.0 so that the scrollview can never be smaller than the image (no matter how far in/out we're zoomed)
[self.scrollView setMinimumZoomScale : 1.0];
[imageView addSubview:button];
}
I thought that this line would solve my problem
//sets the content size to be the size our our whole frame
self.scrollView.contentSize = self.imageView.image.size;
But like I said, it only works after I zoom in or out.
EDIT: When I switch
self.scrollView.contentSize = self.imageView.image.size;
to
self.scrollView.frame = self.imageView.frame;
It works like I want it to (you can't see the background), except the toolbar on the top is covered by the image.

imageView.image.size isn't necessarily the frame of the imageView itself, try setting the
scrollview.frame = imageView.frame
and then
scrollView.contentSize = imageView.image.size
Then you won't see any border. If you want the image to be the maximum size to start with,
do
imageView.frame = image.size;
[imageView setImage:image];
scrollView.frame = self.view.frame; //or desired size
[scrollView addSubView:imageView];
[scrollView setContentSize:image.size]; //or imageView.frame.size

To fix this, I ended up declaring a new CGRect , setting its origin to my scrollView's origin, setting its size with the bounds of my view, and then assigning this CGRect back to my scrollview frame
CGRect scrollFrame;
scrollFrame.origin = self.scrollView.frame.origin;
scrollFrame.size = CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
self.scrollView.frame = scrollFrame;

Related

Rotation around different anchorPoint and position makes image jump to new position first

I'm doing a rotation animation on a view and want it to rotate around the view's center X and bottom Y. I change the anchorPoint and position of the layer and run the animation. Here's my code:
- (void)viewDidLoad {
[super viewDidLoad];
_imageView = [UIImageView newAutoLayoutView];
_imageView.image = [PCImage imageNamed:#"Umbrella"];
[self.view addSubview:_imageView];
[_imageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
[_imageView autoPinEdgeToSuperviewEdge:ALEdgeBottom];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
CGPoint newPosition = CGPointMake(CGRectGetMidX(_imageView.frame), CGRectGetMaxY(_imageView.frame));
NSLog(#"frame %#, new position %#", NSStringFromCGRect(_imageView.frame), NSStringFromCGPoint(newPosition));
_imageView.layer.anchorPoint = CGPointMake(.5, 1.0);
_imageView.layer.position = newPosition;
[UIView animateKeyframesWithDuration:2.0 delay:2.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:.1 animations:^{
_imageView.transform = CGAffineTransformMakeRotation(M_PI / 64);
}];
} completion:nil];
}
edit
The rotation is working, but the view 'jumps' upward to a new position first, where the view's bottom is now where the view's center Y was when first laid out. I thought changing the anchorPoint and updating the position would prevent the jumping. The view is pinned to the superview's bottom edge, and center X to the superview's center X in autolayout, if that might matter. Any ideas?
edit2
I've read other good posts on this like the following but I must be missing something..
Scale UIView with the top center as the anchor point?
I ended up changing the view to not use autolayout after reading this post:
Adjust anchor point of CALayer when autolayout is used
Looks like transforms and autolayout aren't designed to work well together.
_imageView = [UIImageView new];
_imageView.image = [PCImage imageNamed:#"Umbrella"];
_imageView.frame = CGRectMake(0, kScreenHeight - _imageView.image.size.height, _imageView.image.size.width, _imageView.image.size.height);
[self.view addSubview:_imageView];
At some point hoping to experiment with other ideas in that post.

UIView changing the size of the image?

I am trying to add UIImage to UIView .
The image is exactly in the size of the view -as i defined the view rect .
Somehow i see that the images is displayed wider,and taller(its stretched ) .
Why does my view is change the image size ?
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
UIImageView *imageView=[[UIImageView alloc]initWithImage:strip];
UIView * aView = [ [UIView alloc] initWithFrame:CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10) ];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
EDIT :
I can see that my image is 640x960. is it possible that for the iPhone4 the UIImage dont know how to take it and div it by factor 2 ?
Try setting the UIImageView's ContentMode (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html)
imageView.contentMode = UIViewContentModeScaleAspectFit;
use
imageView.layer.masksToBounds = YES;
which will restrict image to be wide and taller
If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).
of course your image is going to be stretched and can not be shown in your view.
Because its frame is a lot bigger than the size of the UIView.
you should set the frame of the UIImageView to be the same size as your UIView, the add it as a subView:
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
CGRect frame = CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10);
// create the uiimageView with the same frame size as its parentView.
UIImageView *imageView=[[UIImageView alloc] initWithFrame:frame];
// set the image
imageView.image = strip;
// create the view with the same frame
UIView * aView = [ [UIView alloc] initWithFrame:frame];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
and of course if you can make the size of the uiimageview smaller ;)

What's the right way to dynamically give height to a UIScrollView depending on the content within?

Here my dilemma. I have 4 elements inside a UIScrollView.
1. Top most element is a UILabel that I give height dynamically depending upon the amount of content in it.
2. Second is a fixed height UILabel that I give position dynamically depending upon the height given to the upper UILabel
3. Third element is a UIImageView that again I have to give position dynamically depending upon the height given to the topmost UILabel
4. The fourth is a UIWebView, to which I gave both, height & position dynamically. (Height depending upon the content in it.. and position again depending on the height of topmost UILabel)
Finally, I dynamically give height to my UIScrollView to accomodate all of the above elements.
Here is the code I use in - (void)webViewDidFinishLoad:(UIWebView *)webView to accomplish all of the above.
//Adjust height of top-most UILabel
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [item.label1 sizeWithFont:label1.font constrainedToSize:maximumLabelSize lineBreakMode:label1.lineBreakMode];
CGRect newFrame = label1.frame;
newFrame.size.height = 0;
newFrame.size.height = expectedLabelSize.height;
label1.frame = newFrame;
//Adjust position of second UILlabel
CGRect labelPosition = label2.frame;
labelPosition.size.height = 20;
labelPosition.origin.y = expectedLabelSize.height +14;
label2.frame = labelPosition;
//Add UIImageView and adjust it's position
UIImageView *image;
image = [[UIImageView alloc] initWithFrame:CGRectMake(0, expectedLabelSize.height +41, 320, 2)];
image.image = [UIImage imageNamed:#"image.png"];
[scrollView addSubview:image];
[image release];
//Adjust UIWebView height and position
CGRect frame = webView.frame;
frame.size.height = 0;
frame.origin.y = expectedLabelSize.height +48;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
//Adjust Scrollview height
scrollView.contentSize = CGSizeMake(320, fittingSize.height +expectedLabelSize.height +48);
Finally, my problem is that when I first load this view, everything but the scrollview get's proper height & position. But, if I go back one view & open this view again, the scrollview has the desired height.
Any ideas what I might be doing wrong here?
My guess is that the scrollView variable isn't yet initialized when this first runs. Try setting a breakpoint somewhere in this code and checking if scrollView has a value or if it's just 0x00000000.

Why after rotating UIImageView size is getting changed?

I'm new in using transformations. And still confusted how they are working.
What I'm trying to do, is to rotate my UIImageView with given angle. But after rotating, it's changing the size of image, getting smaller. I'm also doing scaling for ImageView so it won't be upside down.How to rotate and keep the size, that was given in CGRectMake, when ImageView was allocated ?
UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x,y,width,height)];
myImageView.contentMode = UIViewContentModeScaleAspectFit;
[myImageView setImage:[UIImage imageNamed:#"image.png"]];
myImageView.layer.anchorPoint = CGPointMake(0.5,0.5);
CGAffineTransform newTransform;
myImageView.transform = CGAffineTransformMakeScale(1,-1);
newTransform = CGAffineTransformRotate(newTransform, 30*M_PI/180);
[self.window addSubview:myImageView];
Thanks a lot!
Ok I promised I'd look into it, so here's my answer:
I create a scene which should be somewhat equivalent to yours, code as follows:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2-100,
self.view.bounds.size.height/2-125,
200,
250)];
imageView.image = [UIImage imageNamed:#"testimage.jpg"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
/*
* I added clipsToBounds, because my test image didn't have a size of 200x250px
*/
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];
NSLog(#"frame: %#",[NSValue valueWithCGRect:imageView.frame]);
NSLog(#"bounds: %#",[NSValue valueWithCGRect:imageView.bounds]);
imageView.layer.anchorPoint = CGPointMake(0.5, 0.5);
imageView.transform = CGAffineTransformMakeRotation(30*M_PI/180);
NSLog(#"frame after rotation: %#",[NSValue valueWithCGRect:imageView.frame]);
NSLog(#"bounds after rotation: %#",[NSValue valueWithCGRect:imageView.bounds]);
This code assumes that you are using ARC. If not add
[imageView release];
at the end.
Using this code the logs look like this:
[16221:207] frame: NSRect: {{60, 105}, {200, 250}}
[16221:207] bounds: NSRect: {{0, 0}, {200, 250}}
[16221:207] frame after rotation: NSRect: {{10.897461, 71.746826}, {298.20508, 316.50635}}
[16221:207] bounds after rotation: NSRect: {{0, 0}, {200, 250}}
As you can see the bounds always stay the same. What actually changes due to the rotation is the frame, because an image which has been rotated by 30°C is of course wider than if it handn't been rotated. And since the center point has been set to the actual center of the view the origin of the frame also changes (being pushed to the left and the top). Notice that the size of the image itself does not change. I didn't use the scale transformation, since the result can be achieved without scaling.
But to make it clearer here are some pictures for you (0°, 30° 90° rotation):
They already look pretty similar, right? I drew the actual frames to make it clear what's the difference between bounds and frame is. The next one really makes it clear. I overlayed all images, rotating them by the negative degrees with which the UIImageView was rotated, giving the following result:
So you see it's pretty straight forward how to rotate images. Now to your problem that you actually want the frame to stay the same. If you want the final frame to have the size of your original frame (in this example with a width of 200 and a height of 250) then you will have to scale the resulting frame. But this will of course result in scaling of the image, which you do not want. I actually think a larger frame will not be a problem for you - you just need to know that you have to take it into account because of the rotation.
In short: it is not possible to have an UIImageView which will have the same frame after rotation. This isn't possible for any UIView. Just think of a rectangle. If you rotate it, it won't be a rectangle after the rotation, will it?
Of course you could put your UIImageView inside another UIView which will have a non-rotated frame with a width of 200 and a height of 250 but that would just be superficial, since it won't really change the fact that a rotated rectangle has a different width and height than the original.
I hope this helps. :)
Do not set the contentMode which UIImageView inherits from UIView and leads to the changes in frame according to scaling,transformation,device rotation in accordance to the UIViewContentMode you select.
Also if you just want to rotate you can just change the frame using :-
[UIView beginAnimations:#"Rotate" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
CGRect frame=yourView.frame;
frame.origin.y+=distance you want to rotate;
yourview.frame=frame;
[UIView commitAnimations];
}
if you dont want the animation just change the frame
Try Using This :-
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 0.5f;
animation.repeatCount = HUGE_VALF; // HUGE_VALF is defined in math.h so import it
[self.reloadButton.imageView.layer addAnimation:animation forKey:#"rotation"];

UIScrollView setZoomScale not working?

I am struggeling with my UIScrollview to get it to zoom-in the underlying UIImageView. In my view controller I set
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return myImageView;
}
In the viewDidLoad method I try to set the zoomScale to 2 as follows (note the UIImageView and Image is set in Interface Builder):
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
myScrollView.contentOffset = CGPointMake(941.0, 990.0);
myScrollView.minimumZoomScale = 0.1;
myScrollView.maximumZoomScale = 10.0;
myScrollView.zoomScale = 0.7;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSLog(#"zoomScale: %.1f, minZoolScale: %.3f", myScrollView.zoomScale, myScrollView.minimumZoomScale);
}
I tried a few variations of this, but the NSLog always shows a zoomScale of 1.0.
Any ideas where I screw this one up?
I finally got this to work. what caused the problem was the delegate call being at the end. I now moved it up and .... here we go.
New code looks like this:
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
myScrollView.contentOffset = CGPointMake(941.0, 990.0);
myScrollView.minimumZoomScale = 0.1;
myScrollView.maximumZoomScale = 10.0;
myScrollView.zoomScale = 0.7;
myScrollView.clipsToBounds = YES;
}
Here is another example I made. This one is using an image that is included in the resource folder. Compared to the one you have this one adds the UIImageView to the view as a subview and then changes the zoom to the whole view.
-(void)viewDidLoad{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"random.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[(UIScrollView *) self.view setContentSize:[image size]];
[(UIScrollView *) self.view setMaximumZoomScale:2.0];
[(UIScrollView *) self.view setMinimumZoomScale:0.5];
}
I know this is quite late as answers go, but the problem is that your code calls zoomScale before it sets the delegate. You are right the other things in there don't require the delegate, but zoomScale does because it has to be able to call back when the zoom is complete. At least that's how I think it works.
My code must be completely crazy because the scale that I use is completely opposite to what tutorials and others are doing. For me, minScale = 1 which indicates that the image is fully zoomed out and fits the UIImageView that contains it.
Here's my code:
[self.imageView setImage:image];
// Makes the content size the same size as the imageView size.
// Since the image size and the scroll view size should be the same, the scroll view shouldn't scroll, only bounce.
self.scrollView.contentSize = self.imageView.frame.size;
// despite what tutorials say, the scale actually goes from one (image sized to fit screen) to max (image at actual resolution)
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat minScale = 1;
// max is calculated by finding the max ratio factor of the image size to the scroll view size (which will change based on the device)
CGFloat scaleWidth = image.size.width / scrollViewFrame.size.width;
CGFloat scaleHeight = image.size.height / scrollViewFrame.size.height;
self.scrollView.maximumZoomScale = MAX(scaleWidth, scaleHeight);
self.scrollView.minimumZoomScale = minScale;
// ensure we are zoomed out fully
self.scrollView.zoomScale = minScale;
This works as I expect. When I load the image into the UIImageView, it is fully zoomed out. I can then zoom in and then I can pan the image.