scrollView with UIImage respond touch - ios

I'm using a simple code to add some images to my UIScrollView. Also I've implemented another code to detect touches on each image.
Here is the code:
(void)handleSingleTap:(UIGestureRecognizer *)sender
{
int senderTagIs;
senderTagIs = sender.view.tag;
if (sender.view.layer.borderColor != [UIColor cyanColor].CGColor) {
sender.view.layer.borderColor = [UIColor cyanColor].CGColor;
UIImageView *showFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+100];
[showFullImage setTag:sender.view.tag+200];
[self.view addSubview:showFullImage];
showFullImage.hidden = NO;
NSLog(#"Show tag is: %i", sender.view.tag);
}
else
{
sender.view.layer.borderColor = [UIColor whiteColor].CGColor;
UIImageView *hideFullImage = (UIImageView *)[self.view viewWithTag:sender.view.tag+200];
[hideFullImage setTag:sender.view.tag+100];
hideFullImage.hidden = YES;
NSLog(#"Hide tag is: %i", sender.view.tag);
}
}
The above code, sets the border color to cyan and show my small images from UIScrollView, in another UIImageView.
But my problem is, that I can't set the option to hide all images and set border color white for all images when one image is touched.
Ex: If I touch the first image, then the code will work, my big UIImageView will show touched image and touched image from UIScrollView will get the cyan color for border, so far so good.
Now, If I touch third image, my first image is shown, the color border is cyan, and so... I have to touch first image again to disable, but this is not what I want.

So, we've got a few things going on here. First, I'm assuming you're trying to show a collection of images in a scroll view with some custom padding to indicate selection around them. This sounds tailor make for using UICollectionView with a custom cell.
Absent further information, you're not resetting the old color. Either keep a reference to a selected image as a class variable or, assuming your image views are in a collection object like an NSArray, begin your method by iterating through the objects and resetting their view to an unselected state.
If you just need to hack together a solution, the second option should work. I really recommend using UICollectionView. It's a bit more work in the beginning, especially if you're not experienced with it, but it's well worth learning. Here's a good tutorial on UICollectionView.

Related

Blur for different UIImages in same UITableViewCell

So in my custom UITableViewCell I have 4 different UIImageView's & for each UILongTapGestureRecognizer.
Table View Cells before adding images
After I add images and table looks like this :
Table View Cells after adding images
I need to be able to delete image after long tap gesture recognizer, when the chosen image blurs out and remove button is shown.
I've added the long press gesture recognizer method and while I understand that I need to apply blur to the chosen image and not the UIImageView itself, I dont seem to get how to pass the reference of the picked image to the cell so that I can apply the blur filter to the image.
Is delegate legitimate to use in this case?
In order to apply blur effect as you mentioned you need the image itself.
You can try something like this :)
UIImage *image = [yourImageView image];
UIImage *blurredImage = [UIImageEffects imageByApplyingBlurToImage:image withRadius:30 tintColor:tintColor saturationDeltaFactor:1.8 maskImage:nil];
yourImageView.image = blurredImage;
You can get UIImageEffects apple file from here :)
https://developer.apple.com/library/ios/samplecode/UIImageEffects/Listings/UIImageEffects_main_m.html
Hope it helps :)

Change checkMark color in ios7, Interface Builder

Description
I am developing an application, using tableViews and selecting properties by putting a checkmark for each of selected items. It drove me crazy for 2 hours, because the code was correct, but I didn't see checkmark.
Problem
Actually the checkmark was being loaded correctly but I didn't notice, because the background was white and also the checkmark.
What I want to do
I want to change the color of the checkmark (possibly in Interface Builder) without using custom CheckMarks with different images on it.
Is that possible? If not possible why I am having white checkMark while the other one in another tableView is gray?
It's not possible if you are using accessoryType on cell.
If you want to be really flexible better create custom view instead:
UIImageView* tick = (UIImageView*) [cell.contentView viewWithTag:1];
if(tick == nil)
{
tick = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Archive"]];
[cell.contentView addSubview:tick];
tick.tag = 1;
tick.frame = CGRectMake(285.0f, 20.0f, tick.frame.size.width, tick.frame.size.height);
}
With that you can do whatever you like, use custom images, add background etc...
If you want code to look nicer it will be better if you extend UITableViewCell and create custom class with some methods and properties to control that. I just gave you code to a quick way to achieve what you want.

How to avoid tiling when setting a view's backgroundColor?

I'm trying to set up a background image for my app, but the image is tiling. How do I prevent this? I want my image to fit to the background.
In CSS I would set NO-REPEAT; is there something like that?
I have tried this code:
-(IBAction)btnAddBackground
{
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"image.jpg"]];
}
The documentation for UIView makes a couple of suggestions for this:
Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.

Moving Background of a View

I have a really large image that I want to use as a background image of a view. However, I don't want to display the entire image at once; I want only a part of the image to be displayed, and then I want to animate it to display other parts of it, similar to the "infinite background" in games (only not infinite in my case ;)).
What is the best way to do this? Will I have to separate the image in several pieces and then somehow animate the transition between the pieces, or is there a better way?
How about having UIScrollView as a background view? You can then put UIImageView inside that scroll view and control scroll view's contentOffset as needed.
I found the solution. This piece of code does the magic:
self.backgroundImageView = [[UIImageView alloc] initWithFrame: self.view.bounds];
self.backgroundImageView.image = [UIImage imageNamed:#"background.png"];
self.backgroundImageView.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview: self.backgroundImageView];
The key was setting the contentMode to UIViewContentModeBottomLeft.

Restore default UITableViewCell backgroundView

In short, I need to restore the default backgroundView for a UITableViewStyleGrouped cell.
cell.backgroundView = ??;
The long story:
I have a grouped tableview with some cells. For every cell the user needs to select some value or do some input. After that he can proceed to the next ViewController by touching a "next" button.
Anyway, to indicate to the user that he missed something, I display a nice red border around the cell with the wrong/missing user input. I do that by setting the backgroudView of the cell with a custom image like this:
cell.backgroundView = myErrorIndicatingImageView;
The cell now looks like this (screenshot, ignore the stars and label)
So far so good, this works like a charm. But after the user corrects the input I want to remove my red border background image and just show the original background again. The original background looks like this (screenshot):
And this seems to be a problem.
I tried several things
// try by setting the background to nil
cell.backgroundView = nil;
this removes the background completely and I'm lost with a cell without background.
// try it with addSubview and later remove the subview again
[cell.backgroundView addSubview:myErrorIndicatingImageView];
this does nothing. The myErrorIndicatingImageView is not visible. Even a [cell.backgroundView bringSubviewToFront:myErrorIndicatingImageView] does not help.
Right now the only solution I have is to create a new UITableViewCell for every single call to cellForRowAtIndexPath. This works but is just bad code and ignores completely the technique to reuse cells.
There must be a simpler solution...something like
cell.backgroundView = [self.tableView getDefaultBackgroundView];
what about trying this:
cell.backgroundView = [[UIView alloc] initWithFrame:cell.backgroundView.frame];
Or you can make the background view as a UIImageView and set 2 images in problem and fixed
Assuming you are doing this with a custom table cell class, save the original backgroundView in some ivar, then apply the new background. When you want to reset the background, set the backgroundView back to the original, saved background view.

Resources