Animation of contentmode of a uiview - ios7

I'm trying to override the animation of contentmode of a uiview. How can I do that within a uiviewcontroller.
Any help will be apreciated,
Happy holidays to all.

Related

touchesBegan in UIView not being called

I know that this question must have been answered plenty of times already, but I have no idea why the touchesBegan: is not called in my UIView.
Before I added it in my app, I made a trial project to do some tests. What I did was subclass a UIView where the touchesBegan: is implemented and add this as a subview of another view. The superview has a button which shows the subclassed UIView once clicked. Everything works as expected.
However, when I did the same thing in my existing app, the touchesBegan: method, as well as the touchesMoved:, were never called. Can anyone explain what could be preventing the methods from being called?
I just found the answer to this. I realised that the superview of the UIView has the userInteraction set to disabled (which is the default value). So when I enabled the userInteraction of the superview, the touches are now recognised.
In my case background = UIColor.clearColor() was the reason why touchesBegan was not called. Its obviously not called on transparent elements.
I was struggling on this problem for a while... and i figure it out. I think that the touchesBegan override function listen on the main view: the first one on the storyboard tree of the ViewController! I would love to post an image of my storyboard to be more clear, but i can't! ;)
Anyway, IF you have subviews, they may covers the main view... in this manner the touch wont "reach" the main view.
To avoid this you'll have to set the Background property in the Attribute Inspector to Default for ALL THE SUBVIEWS. In this way the background will be transparent and the touch will be able to reach the main view.
If you don't want to set the background to transparent there is something you can do:
-Add an outlet of your last view (with background set)
-Add a tap gesture recognizer to it
-Handle the tap disposing the keyboard
ES.
In properties:
//The last view's outlet
#IBOutlet weak var ContainerView: UIView!
In viewDidLoad():
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
self.ContainerView.addGestureRecognizer(tap)
Outside:
func handleTap(recognizer: UITapGestureRecognizer){
self.view.endEditing(true)
}

UIScrollview events will be caught by included UIButtons

I implemented a UIScrollview and added a lot of UIButtons inside. But now there is only a little space for scroll within the scrollview.
If I try to scroll the scrollview on a UIButton the scrollview won't move. The cause for that is that the UIButtons catch the necessary events for the scrollview.
Could anybody help me to solve the issue?
You need to set canCancelContentTouches on your UIScrollView. This lets the UIScrollView class allow touches within it's subviews.
[myScrollview setCanCancelContentTouches:NO];

subclassing a UITableViewCell with UIImageView in it

I have subclassed a UITableViewCell and inside I have a UIImageView, the problem is that when I tap on the UIImageView, I wanted to do something, but what happens now is that it calls the didSelectRowAtIndexPath. How do I prevent this? Also, should I just add a tap gesture recognizer to the UIImageView to perform some action I want?
Can you use a UIButton with your image instead of a UIImageView? UIButton has all the UIControl functionality.
I'm trying to remember if that's enough to avoid didSelectRowAtIndexPath and I don't remember though I think it is. Do you still want the row to be selectable by tapping outside the image? If not, you can just return NO to canSelectRowAtIndexPath.
Yes simply add a UITapGestureRecognizer on the UIImageView is the way to go.
Don't forget to set userInteractionEnabled to YES on your UIImageView too.
Then, when you will tap on your UIImageView, the touch event will be captured by its UITapGestureRecognizer and won't be transmitted to the cell, so the method associated with your UITapGestureRecognizer will be called but the tableView:didSelectCellAtIndexPath: delegate method won't. (This latter delegate method will only be called if the touch event is not captured by a subview of your cell before)

How to access subviews in nested UIScrollViews

I was having trouble getting a UIScrollView with multiple UIImageView subviews to zoom properly so I tried using a UIScrollView with multiple UIScrollView subviews inside of it and a UIImageView inside of each of those. That way one UIScrollView can only scroll and the other can only zoom.
My question is: How can I access subviews of a subview. I know that normally I can access subviews using [scrollView viewWithTag:tagInt]; for example, but I can't seem to access the subviews of a subview using [[scrollView viewWithTag:tagInt] viewWithTag:tagInt2]; since viewWithTag only returns a single UIView and not all of it's subviews.
I could always give each subview a unique tag and access them that way, but that doesn't seem to be the most elegant solution.
What is the best way to access a subView and then get to the subView's subview (ie: access my UIScrollView used for zooming which is a subview of my main view, and then access it's subView UIImageView)?
If you don't feel comfortable subclassing for now, you'd have to assign tags to the UIScrollViews containing images. You'd then have to do what the BobC suggested with a little more work.
for (UIView *subview in [myScrollView subviews])
{
//check if the current subview is one of the UIScrollViews
if (subview.tag > 100)
//do something with the UIScrollView
}
The most elegant way to do this would be to subclass UIScrollView, give it a UIImageView property. That way, you could access this UIImageView with something like:
MyScrollView *myScrollView = (MyScrollView *)[scrollView viewWithTag:tagInt];
UIImageView *imageView = myScrollView.imageView;
Hope this helps!
You can get an array of subviews of any UIView using subviews property.
for(UIView *subview in [myScrollView subviews]) {
// do anything with your subview here.
}

How do I draw text into a subview?

I want to draw text into UIView's subview using drawInRect:withFont:lineBreakMode call but that operates on the current context only.
Is it possible to draw text into a subview from current view?
The subview is a generic UIView instance and I don't really want to create a new UIView-derived class just for this purpose if I can avoid it.
One option would be to add a CALayer to the view's layer instead of adding a UIView to the view. The CALayer has a delegate property which you can assign any object to. The CALayer calls:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
on the delegate, which you can take to do something like:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
if (layer == myLayer) {
UIGraphicsPushContext(ctx);
[string drawInRect:rect withFont:font lineBreakMode:mode];
UIGraphicsPopContext();
}
}
No, if you're going to do something with a context, you have to be in that view's -drawRect:. You can always make your subview a UIView subclass that overrides -drawRect: to display the text you want... but at that point, you're kind of reinventing UILabel.
No, you can't do what you describe. Subclassing UIView is exactly the method you're supposed to use for this— there's nothing wrong with creating a UIView subclass which only has a simple -drawRect: method.