I have created a 3 Subviews to MyView by creating new class inherits from UIView.
then i tried to change text in a particular subview say second sub view.
But its not updating.So how to do this...
EDIT
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CALayer *hitLater = [self layerForTouch:[touches anyObject]];
if ([hitLater isKindOfClass:[Tile class]])
{
Tile *tile = (Tile *)hitLater;
heldTile = tile;
[tile draw2];
[tile setNeedsDisplay];
}
}
Thanks in advance..
If you want a better answer show some code and how you add the text to your UIView subclasses etc.
But maybe you have to call setNeedsDisplay on your subviews so they get a redraw.
Related
I have created custom navigation bar and added one UIButton over it.
Please take a look at image attached.
I have UIButton with background image added as subview to Custom NavigationBar so portion above GREEN line is clickable not the portion below green line.
I have tried using UITapGestureRecognizer and also the touches methods ie. touchesBegan to UIImageView to handle the touch, but no luck.
I think this is only because my UIButton nontouchable potion is outside of NavigationBar Frame.
Is there any way to click the subview's portion which is ouside its parent view.
The parent view size is less than the Child view . That's why it is non clickable. Asper my knowledge only option you have is try to increase the parent view (Custom Navigationbar) frame size .
Yo need subclass Navigation Bar and add method hitTest:withEvent:. For example:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
CGRect extraTapRect = ...; // Put here needed rect
if (CGRectContainsPoint(extraTapRect, point)) {
for (UIView *subview in self.subviews.reverseObjectEnumerator) {
CGPoint subviewPoint = [subview convertPoint:point fromView:self];
UIView *result = [subview hitTest:subviewPoint withEvent:event];
if (result != nil) {
return result;
}
}
}
return [super hitTest:point withEvent:event];
}
I have several UIImageView as subview of a UIView that act as a canvas. The ImageViews receive touch events, so I can move them.
If I pan two views, each finger on each of them I can move two views at the same time. I don't want that.
I checked the maximumNumberOfTouches property on the superview but it affects that view, but it doesn't prevent that each other subview receives the touch event.
Any ideas how to avoid this behavior?
Thanks
How about just setting a flag that an image view is already moving, and the pan gesture on all views check that flag before actually moving their views? Just set the flag to false once the view has stopped moving.
you may do like this , extend UIImageView as a Custom UIView may called YouImageView
in the touch event
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for ( UIView *v in self.superview.subviews )
{
if ( v != self )
{
v.userInteractionEnabled = NO;
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for ( UIView *v in self.superview.subviews )
{
v.userInteractionEnabled = YES;
}
}
done!
I have added one scrollview and imageview to my view. When I touch on this imageview my scrollview scrolls. And within my scrollview there are lots of imageviews which have touch events written separately.
Here is the view hierarchy
View
|
|
------- --------
| |
imageView1 scrollview
|
...........................
| | |.....|
imageviews2
Imageview1 is above imagesview2 in appearance.
And when I scroll the scrollview it is not possible to touch some of the imageViews within the scrollview as it comes behind the imageview within view.
How can I pass the touch events to the imageviews2 whenever it is behind imageview1.
You'll need to subclass UIView and override
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
in that subclass to skip imageView1.
You might get away with subclassing UIImageView and instantiating imageView1 as a member of that class, and overriding pointInside
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{ return NO; }
don't forget also to uncheck "delays content touches" and "cancellable content touches" on your scrollview !
I hope this answer might be useful for someone. I have done it without using hittest.
Took all the subviews in scrollview and checked if it is an image view and did the following.
Passed the touchevents to another view using [view touchesBegan:touches withEvent:event]; we have to write the same in touchesMoved and touchesEnded also.
Thanks,
Joku
You need to pass the touch event to the UIViews that are outside the bounds of the parent view.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
// Convert the point to the target view's coordinate system.
// The target view isn't necessarily the immediate subview
CGPoint pointForTargetView = [self.targetView convertPoint:point fromView:self];
if (CGRectContainsPoint(self.targetView.bounds, pointForTargetView)) {
// The target view may have its view hierarchy,
// so call its hitTest method to return the right hit-test view
return [self.targetView hitTest:pointForTargetView withEvent:event];
}
return [super hitTest:point withEvent:event];
}
https://developer.apple.com/library/ios/qa/qa2013/qa1812.html
I have a uiview at the top of the interface (below the status bar) that only the bottom part of it is shown.
Actually, I want to make the red uiview to slide down to be entirely shown by drag such as the notificationcenter in the native iOS and not just by taping a button.
What should I use to "touch and pull down" the uiview so it could be shown entirely ?
No needs to find a workaround of drag-n-drop. An UIScrollView can do it without any performance loss brought by listening on touches.
#interface PulldownView : UIScrollView
#end
#implementation PulldownView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (!self) {
return self;
}
self.pagingEnabled = YES;
self.bounces = NO;
self.showsVerticalScrollIndicator = NO;
[self setBackgroundColor:[UIColor clearColor]];
double pixelsOutside = 20;// How many pixels left outside.
self.contentSize = CGSizeMake(320, frame.size.height * 2 - pixelsOutside);
// redArea is the draggable area in red.
UIView *redArea = [[UIView alloc] initWithFrame:frame];
redArea.backgroundColor = [UIColor redColor];
[self addSubview:redArea];
return self;
}
// What this method does is to make sure that the user can only drag the view from inside the area in red.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (point.y > height)
{
// Leaving useless touches to the views under it.
return nil;
}
return [super hitTest:point withEvent:event];
}
#end
How to use:
1. Initialize an instance of PulldownView.
2. Add any content you want to display to the instance using [addSubview:].
3. Hide the area in red.
[pulldownView setContentOffset:CGPointMake(0, heightOfTheView - pixelsOutside)];
This is a simple example. You can add any features to it like adding a titled button bar on the bottom of the draggable area to implement click-n-drop, or adding some method to the interface to reposition it by the caller.
Make a subclass of UIView.
Override touchesBegan:withEvent and touchesMoved:withEvent.
In the touchesBegan perhaps make a visual change so the user knows they are touching the view.
In the touchesMoved use
[[touches anyObject] locationInView:self]
and
[[touches anyObject] previousLocationInView:self]
to calculate the difference between the current touch position and the last touch position (detect drag down or drag back up).
Then if you're custom drawing, call [self setNeedsDisplay] to tell your view to redraw in it's drawRect:(CGRect)rect method.
Note: this assumes multiple touch is not used by this view.
Refer to my answer in iPhone App: implementation of Drag and drop images in UIView
You just need to use TouchesBegin and TouchesEnded methods. In that example, I have shown how to use CGPoint, Instead of that you have to try to use setFrame or drawRect for your view.
As soon as TouchesMoved method is called you have to use setFrame or drawRect (not sure but which ever works, mostly setFrame) also take the height from CGPoint.
I'm using UIImageViews. I'd like this behaviour: if we touch the UIImageView it needs to zoom double and if the user remove his finger set the UIImageView to the actual position of the finger... How can I do?
Can any one please tell me the code for this?
Thanks in advance
U can use UIScrollView for this, with UIImageView as a sub-view of the scroll view.
For this, U need to sub-class UIScrollView to write a custom scroll view.
It will have all the properties of UIScrollView. Only thing, U have to override touchesEnded: method, which will notify the superview. So in the superview, again u can set the zoomscale to 1, to make it to actual position.
implement the delegate methods touchesBegan:withEvent: and – touchesEnded:withEvent:
in touches began method change the frame of the imageview to double the size of the imageview and in touchesEnded change the frame to the original size.
while declaring the image enable user interaction on it
UIImageView *imageview=[[UIImageView alloc] init];
imageview.userInteractionEnabled=YES;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
imageview.frame=CGRectMake(0,0,20,20);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
imageview.frame=CGRectMake(0,0,40,40);
}
TNX