Hello fellow programmers.,
I want to create dynamic UIView of size 100X100 around my double click on Main UIView (Everytime I double click diffrant place). I totally don't have any idea about it. So can't provide any code for it. Anyone can help??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self.view];
NSLog(#"Point in myView: (%f,%f)", currentPos.x, currentPos.y);
UIView*hover = [[UIView alloc] initWithFrame:CGRectMake(currentPos.x - 50, currentPos.y - 50 , 100, 100)];
hover.backgroundColor = [UIColor grayColor];
[self.view addSubview:hover];
}
try this code
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addDynamicView:)];
self.view.userInteractionEnabled = TRUE;
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
adding the custom view
- (void)addDynamicView:(UITapGestureRecognizer*)aGesture{
/** add view with specified frame **/
CGRect rect = CGRectMake(10, 10, 100, 100);
UIView *lView = [[UIView alloc] initWithFrame:rect];
lView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lView];
}
Related
I want to drag my label in Application. I create new label when starting Application
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
myLabel.text = #"DRAG ME!!!";
[self.view addSubview:myLabel];
How can I drag this label? I can't find normal tutorial and i new in Xcode...
The following implementation may help you. Change your above label with this one and you can see that now your label is draggable.
#import "DraggableLabel.h"
#interface DraggableLabel()
{
CGPoint _previousLocation;
}
#end
#implementation DraggableLabel
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:#selector(handlePan:)];
self.gestureRecognizers = #[panRecognizer];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.superview bringSubviewToFront:self];
_previousLocation = self.center;
}
- (void)handlePan:(UIPanGestureRecognizer *)panGestureRecognizer
{
CGPoint translation = [panGestureRecognizer translationInView:self.superview];
self.center = CGPointMake(_previousLocation.x + translation.x,
_previousLocation.y + translation.y);
}
#end
I have a custom view that will pinch zoom, but I'd like to also slide it around in any direction. The zoom works well, and the moving works well, but so far it's just one or the other. The touchesMoved method never gets called.
The scrollView has a subView which the user will touch and move. It will function like a little map. On the subView are several custom labels. I have a tap Gesture recognizer on the labels, which works fine now, and will still need to work when the zoom and moving is corrected.
I also tried [scrollView setScrollEnabled:NO]; and other things, but that didn't allow both to work. How can touches Moved be called? What will I need to do to allow both to work?
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setUserInteractionEnabled:YES];
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[scrollView setMinimumZoomScale:.7];
[scrollView setMaximumZoomScale:1.5];
[scrollView setAutoresizesSubviews:YES];
[scrollView setUserInteractionEnabled:YES];
scrollView.delegate = self;
[self.view addSubview:scrollView];
CGRect mapFrame = CGRectMake(0, 0, 300, 300);//temp numbers
subView = [[UIView alloc]initWithFrame:mapFrame];
subView.userInteractionEnabled=YES;
subView.autoresizesSubviews=YES;
[scrollView addSubview:subView];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touchesMoved");
touch = [touches anyObject];
CGPoint location = [touch locationInView:subView];
CGPoint prevLoc = [touch previousLocationInView:subView];
CGPoint origin = subView.frame.origin;
origin.x += (location.x - prevLoc.x);
origin.y += (location.y - prevLoc.y);
CGRect newFrame = subView.frame;
newFrame.origin = origin;
subView.frame = newFrame;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return subView;
}
//label sample
newCity =[[MyCustomClass alloc] initWithFrame:CGRectMake(x, y, 95, 40)];
newCity.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(touchUp:)];
[newCity addGestureRecognizer:tapGesture];
[subView addSubview:newCity];
I'm also getting this in the log and don't know why. "Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since gesture recognizer is not active.
" I removed the pan gesture recognizer, after that didn't work. I'm using Xcode 4.5.2
My header is
#interface CitiesViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>
{
UIView *subView;
}
#property(nonatomic, strong) UIView *subView;
#property(nonatomic, strong) UIScrollView *scrollView;
To solve this I removed the TouchesMoved entirely and used a panGestureRecognizer instead. Hopefully it helps someone.
I added this to my subView:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan:)];
[pan setDelegate:self];
[subView addGestureRecognizer:pan];
Add this method:
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:subView];
subView.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
}
I never did figure out why that message Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] is still occurring, but this seems to work.
I have a UIScrollView .In that i have one ImageView.
I want to get UITOUCH point .
i know i can get location by
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}
But i want uitouch on image.
PLease help me.
try like this,
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: tapRecognizer.view]
}
AND set image view userInteractionEnabled to YES
UIImageView *image = [[UIImageView alloc]init];
image.frame = CGRectMake(55, 30, 50, 40);
image.image = [UIImage imageNamed:#"Default.png"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
singleTap.numberOfTapsRequired = 1;
self.view.tag =10;
image.userInteractionEnabled = YES;
[image addGestureRecognizer:singleTap];
I need to handle touch on my object when it move across my screen. When touchesBegan called I need to hide my object.
this is my code in UIViewController:
- (void)win {
for (NSInteger i = 1; i <= 6; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"rocket_%d.png", i]]];
imageView.tag = 1000;
CGSize size = imageView.image.size;
[imageView setFrame:CGRectMake(200 + (i * 60), 500, size.width, size.height)];
[self.view addSubview:imageView];
[rockets addObject:imageView];
[imageView setUserInteractionEnabled:YES];
}
[self startRockets];
}
- (void)startRockets {
CGFloat timeInterval = 1.0;
for (UIImageView *imageView in rockets) {
[UIView animateWithDuration:5.0 delay:timeInterval options:UIViewAnimationOptionAllowUserInteraction animations:^{
[imageView setFrame:CGRectMake(imageView.frame.origin.x, 0, imageView.frame.size.width, imageView.frame.size.height)];
} completion:nil];
timeInterval += 1.0;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSArray *views = [self.view subviews];
for (UIView *v in views) {
if (v.tag == 1000) {
if (CGRectContainsPoint(v.frame, touchLocation) == YES) {
[v setHidden:YES];
}
}
}
}
Maybe you are missing the AllowAnimatedContent. Try changing your
UIViewAnimationOptionAllowUserInteraction
to
UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent
...another way might be to use UITapGestureRecognizer :
- (void)createViews {
rockets = [[NSMutableArray alloc] init];
for (NSInteger i = 1; i <= 6; i++) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake((i * 60), 100, 50, 50);
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(rocketTouched:)];
[view addGestureRecognizer:tap];
[self.view addSubview:view];
[rockets addObject:view];
}
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(moveViews) userInfo:nil repeats:YES];
}
- (void)moveViews {
for (UIView * view in rockets) {
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionAllowAnimatedContent animations:^{
view.frame = CGRectMake(view.frame.origin.x -10, 0, view.frame.size.width, view.frame.size.height);
} completion:nil];
}
}
- (void)rocketTouched:(UITapGestureRecognizer*)tap {
tap.view.hidden = YES;
}
You need also check this if you need to detect intersection with object.
I have edited touchesBegan method and it works.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIImageView *imageView in rockets) {
CALayer *bbl1ObjPresentationLayer = (CALayer*)[imageView.layer presentationLayer];
if(CGRectIntersectsRect(bbl1ObjPresentationLayer.frame, CGRectMake(touchLocation.x, touchLocation.y, 1, 1))) {
NSLog(#"intersect");
//They are intersecting
}
}
}
if you do it using CALayer insted of UIImageView object this work correct. Check this answer link
This is my code:
- (void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self.view];
startLocation = pt;
[super touchesBegan: touches withEvent: event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self.view];
CGRect frame = [self.view frame];
frame.origin.x += pt.x - startLocation.x;
frame.origin.y += pt.y - startLocation.y;
[self.view setFrame:frame];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *v = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)];
v.backgroundColor = [UIColor redColor];
UIImageView *imv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"boo2.png"]];
imv.frame = CGRectMake(0, 0, 50, 50);
[v addSubview:imv];
[self.view addSubview: v];
[v release];
UIView *v2 = [[UIView alloc] initWithFrame: CGRectMake(100, 100, 100, 100)];
v2.backgroundColor = [UIColor blueColor];
UIImageView *imv2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"boo2.png"]];
imv2.frame = CGRectMake(0, 0, 50, 50);
[v2 addSubview:imv2];
[self.view addSubview: v2];
[v2 release];
}
What I want to do is to move the UIImageView inside the UIView, not the entire view. But, the code above, when I try to drag, it moves the entire view.
NOTE: The images are return from others functions, not declared in viewdidload. Assume, you cant declare UIImageView variables.
I'm not quite sure what you're trying to do. and I don't know why you put each of the UIImageView's inside a UIView which you put into the view controller's view. You can just as well add the UIImageView's as sub-views of the view controller's view directly.
Nevertheless, from what I can make out it looks like the user must be able to drag the images on the screen around.
If this is the case, I would suggest that you store each one of the UIImageView's in an array. Then, in touchesBegan:withEvent:, you will have to determine which UIImageView is being touched. Getting the CGPoint representing the location in the main view, as you do in your code, is a good start. If you added the UIImageView's directly as sub-views of the main view, you can then compare the coordinates of this CGPoint with the frames of these UIImageView's to determine which one is being pressed.
Then, to allow dragging, you need to save the coordinates of the UIImageView at the beginning of the touch. Then, everytime the touch moves, you can compute the new x-position of the UIImageView as the original x-position, plus the distance moved. That is,
newImageViewX = imageViewStartX + (newTouchX - touchStartX);
and similarly for the y-position.