How to mask a UIScrollView? - xcode

I currently have an image mask I want to use to mask a UIScrollView.
The scrollview holds 1 UIImageView.
Here is what I do at the moment in viewdidload:
CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:#"ScrollMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 512, 384);
[Scroll1.layer setMask:mask];
This works to some degree.
It masks the ImageView inside the scrollview but not the scrollview itself.
Is there a way to mask the scrollview's CALayer and not imageview's layer?

You should put the UIScrollView into a UIView, and then apply the mask to the UIView instead of the UIScrollView.
CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:#"ScrollMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 512, 384);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 512, 384)];
[containerView.layer setMask:mask];
[containerView addSubview:Scroll1];
[self.view addSubview:containerView];

Related

Xcode UIImageView in UIScrollView IOS

can you help me? I would like add background image with scrolling, because my background image is too high. How can I do this? I add UIScrollView and into I add UIImageView but it doesnt scroll.
Thank you for replies.
Here is working code:
- (void)viewDidLoad
{
[super viewDidLoad];
//create scrollView and set the frame to the size you want.
//In this example the scrollView frame is the whole ViewController size
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setBackgroundColor:[UIColor clearColor]];
//create a UIImage,set the imageName to your image name
UIImage *image = [UIImage imageNamed:#"YourImageName.png"];
//create UIImageView and set imageView size to you image height
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, image.size.height)];
[imageView setImage:image];
//add ImageView to your scrollView
[scrollView addSubview:imageView];
//set content size of you scrollView to the imageView height
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, imageView.frame.size.height)];
[self.view addSubview:scrollView];
}
If you want the scroll view to scroll, set the content size to be bigger than the scroll view frame.

UIScrollview not scrolling what am i doing wrong

I have an issue where i am adding a UIScrolview to my UIViewController, but my UIScrollview doesnt want to scroll. I want it to work the same way that it works on a UITableViewController.
Here i initialize my UIScrollview
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
scroll.contentSize = CGSizeMake(320, 800);
scroll.backgroundColor = [UIColor redColor];
scroll.showsHorizontalScrollIndicator = YES;
scroll.scrollEnabled = YES;
scroll.userInteractionEnabled=YES;
[self.view addSubview:scroll];
Here i add my content to the scrollview:
UILabel *memberNo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
memberNo.text = #"Member No";
memberNo.font = [UIFont boldSystemFontOfSize:16];
memberNo.textColor = [UIColor limeColor];
[scroll addSubview:memberNo];
memberText = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
memberText.placeholder = #"Member No";
memberText.borderStyle = UITextBorderStyleRoundedRect;
memberText.text = #"1111111";
[scroll addSubview:memberText];
Try this
set content size more than the screen size and it will scroll whether there is more content or not
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
HorizontalScroll
scroll.contentSize = CGSizeMake(320, 900);
VerticalScroll
scroll.contentSize = CGSizeMake(340, 800);
As others have said, the scroll view's size (specifically, its bounds.size) must be smaller than its contentSize for it to actually scroll.
You probably want your scroll view to fit inside its superview, so try setting its frame to the superview's bounds:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
The content is the same size as the scrollview: there is nothing to scroll.
The scrollview needs to contain a view that is bigger than it's own size.
Try setting the contentSize to be larger than the frame. For example:
scroll.contentSize = CGSizeMake(1000, 800);
If that doesn't work, then check where you are setting the content size. Where are you initializing the UIScrollView? Try moving the above line to viewWillAppear or viewDidLayoutSubviews.
frame of the scroll view refers to the size of the scroll view itself whereas content size is the size for the view which is to be shown in the scroll view. If the size of view to be shown in scroll view is having smaller size than the scroll views frame size, then it won't at all scroll, but if you set content size more then scroll view has to scroll to show the 'extra' size more than its own frame size.
e.g. If height of scroll view is 100 and you have set the height factor in content size equal to 150, then these extra 50 pixels will be scrollable.
Good Luck!

How to add image to view programmatically?

Say you have a UIImage *image and a UIView *v
How would you display the image on top of the view programmatically?
If you just want to add the UIImage to the UIView then you need an UIImageView inbetween the UIView and UIImage, such as:
UIImage *image = [[UIImage alloc] init];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[iv setImage:image];
[v addSubview:iv];
Note that the above is just dummy code and I've created all UI elements with a zero frame.
If you want to add UIImage to UIView programatically you can directly add image to view, Such as:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"YourImage.png"]];
[self.view addSubview:customView];

How to stretch (scale) image on a UIView?

How can i take an image and stretch it to fill the screen on my UIView (320/460)
I am loading and placing it, however as you can see, some scaling is needed.
Thank you!
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[myImageView setContentMode:UIViewContentModeScaleToFill];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
Like I said in my comments above to Shamsiddin's post, the UIImageView needs a size to know where to stretch to. Here I'm using the same size of the UIView. I've added another answer because the code to UIView is missing. So here's the complete code.
// Area of the UIView and UIImageView
CGRect gameArea = CGRectMake(0, 0, 320, 460);
// Create UIImageView with Image
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
// Create UIView and insert subview
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
[self.view addSubview:myImageView];
try
UIView *view;
view.ContentMode = UIViewContentModeScaleToFill;
I just did this in my own iOS5 app, i needed to dynamically set the background image and scale it. Imagine you are doing this in - (void)viewWillAppear:(BOOL) animated
UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setImage:[UIImage imageNamed:#"Background.png"]];
[self.view addSubview:imageview];
[self.view sendSubviewToBack:imageview];
So we create the view, set it's scaling mode, add the bg image, set it as the subview in the main view and then push it to the back so it does not occupy any other subviews.
Please accept this answer.

UIScrollView viewForZoomingInScrollView when pagingEnabled with multiple UIImageViews in it

I have this UIScrollView with pagingEnabled to let the user page between some images.
Now I want to let the user zoom on each one of the images. How do I do that? What I have now zooms on my UIScrollView from its origin and I need it to zoom on each picture, not the hole UIScrollView.
What I have now:
UIScrollView* containerView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
containerView.scrollEnabled = YES;
containerView.pagingEnabled = YES;
[containerView setMaximumZoomScale:2.0];
[containerView setDelegate:self];
self.view = containerView;
UIImage *imageOne = [UIImage imageNamed:image01];
UIImageView *viewOne = [[UIImageView alloc] initWithImage:imageOne];
viewOne.frame = CGRectMake(0, 44, 320, 480);
[containerView addSubview:viewOne];
UIImage *imageTwo = [UIImage imageNamed:image02];
UIImageView *viewTwo = [[UIImageView alloc] initWithImage:imageTwo];
viewTwo.frame = CGRectMake(320, 44, 320, 480);
[containerView addSubview:viewTwo];
UIImage *imageThree = [UIImage imageNamed:image03];
UIImageView *viewThree = [[UIImageView alloc] initWithImage:imageThree];
viewThree.frame = CGRectMake(640, 44, 320, 480);
[containerView addSubview:viewThree];
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)containerView
{
return containerView;
}
Thanks in advance
You have to return a view contained in your scrool. As you have 3 UIImage, try with this...
In .h
UIView *auxView;
In .m
...
[auxView addSubview:imageOne];
[auxView addSubview:imageTwo];
[auxView addSubview:imageThree];
[containerView addSubview:auxView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)containerView{
return auxView; }
I hope this help you
I think you mean you want to zoom each UIImageView individually, right?
I was struggling with this problem many weeks, then I decide to wrote a class to solve this problem.
Here is my github repo of this class: https://github.com/windmemory/PhotoCollectionView
My solution to this problem is to create single UIScrollView to contain every UIImageView, and then add these UIScrollView to the containerView.
If we add all the UIImageView to a single view then make it zoomable, when we zoom one photo, all these photos will be zoomed, then the contentSize will change accordingly. Once the contentSize is changed, it will messed up all the paging thing. So the best way I came up is to use UIScrollView to contain UIImageView, then put every UIScrollView into the containerView, this will allow you to zoom each photo separately, which won't mess up all the paging thing you set up.
Hope this may help you.

Resources