Play a sound when UIImageView are inside a specific area - uiimageview

I am moving around some UIImageview with UITouch.
I want to play a sound when the image are inside a specific area. What is the easiest way to do that?
Thanks for any answer.
// ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize imageView1,imageView2;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == imageView1) {
imageView1.center = touchLocation;
}
else if ([touch view] == imageView2) {
imageView2.center = touchLocation;
}
}
#end
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
UIImageView *imageView1;
UIImageView *imageView2;
}
#property(nonatomic,retain) IBOutlet UIImageView *imageView1;
#property(nonatomic,retain) IBOutlet UIImageView *imageView2;
#end

I solved it like this:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSLog(#"%#", NSStringFromCGPoint(touchLocation));
[self.view bringSubviewToFront:imageView1];
if ([touch view] == imageView1) {
imageView1.tag=0;
if ((touchLocation.x >= 340 && touchLocation.x <= 420) && (touchLocation.y >= 185 && touchLocation.y <= 265)) {
NSLog(#"head");
[UIView animateWithDuration:0.35
delay:0.0
options: UIViewAnimationOptionAllowAnimatedContent
animations:^{
imageView1.frame = CGRectMake(380.0-43, 225.0-54, 90.0, 108.0);
imageView1.tag=1;
}
completion:^(BOOL finished){
NSLog(#"Auto justerad!");
}];
}
}
}

Related

draggable specific class which is a subclass of UIImageView

I have create a class Box which is a subclass of UIImageView.
In my program, I want to make the Box to be the only draggable object in the main view controller.
Here is my code:
BoxViewController.m
-(void) viewDidLoad{
[super viewDidLoad];
target = [[Box alloc] initWithFrame:CGRectMake(10,10,100,100)];
[self.view addSubView:target];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == target){
CGPoint location = [touch locationInView:self.view];
target.center = location;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event];
}
I think the condition is not working, but I have tried isKindOfClass. Both didn't work. What is the solution? Thank you very much.
try this method
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint location = [[touches anyObject] locationInView:self.view];
target.transform = CGAffineTransformMakeTranslation(location.x, location.y);
}

Expected identifier image

Hi i am new here and also a noob with Xcode please help.
I am trying to make Imagepickercontroller but i am having trouble with one error and don't know how to solve it.
Viewcontroller.h
#interface ViewController : UIViewController<UIImagePickerControllerDelegate> {
IBOutlet UIButton *button;
IBOutlet UIImageView *image;
UIImagePickerController *imagepicker;
}
- (IBAction)change:(id)sender;
#end
Viewcontroller.m
#interface ViewController ()
#end
#implementation ViewController
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch = [[event allTouches] anyObject];
button.center = [mytouch locationInView:self.view];
}
- (void)viewDidLoad
{
[super viewDidLoad];
imagepicker = [[UIImagePickerController alloc] init];
[imagepicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[imagepicker setAllowsEditing:YES];
}
- (IBAction)change:(id)sender
{
[self presentViewController:imagepicker animated:YES completion:NO];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[UIImage(setimage:)image];
[self dismissViewControllerAnimated:YES completion:NO];
}
#end
Expected identifier [UIImage(setimage:)image];
[UIImage(setimage:)image] needs to be [someImageView setImage:image]
Where someImageView is an instance of UIImageView
Note that UIImage is a class, not a object. You should give a class method of UIImage without any parenthesis.
In addition, setImage: is not a class method of UIImage. I guess, you mean a object instance, not the class object.

UIBezierPath different colors

I was able to draw with UIBezierPath in my app. Now I added a UIColor variable so the user, pushing a button, can select a different color. When the user change the color and he start drawing with the new color, all the paths already drawn have the new color.
How can I tell the object to not change the color of the already drawn paths?
Here is my code. Any example of code is much appreciated! :)
(I have my UIColor variable storaged in the appDelegate and I am calling the refresh method from another viewControlle.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.backgroundColor = [UIColor whiteColor];
myPath = [[UIBezierPath alloc] init];
myPath.lineCapStyle = kCGLineCapRound;
myPath.miterLimit = 0;
myPath.lineWidth = 5;
brushPattern = app.currentColor;
}
return self;
}
-(void)refresh {
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
brushPattern = app.currentColor;
}
- (void)drawRect:(CGRect)rect {
[brushPattern setStroke];
for (UIBezierPath *_path in pathArray)
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
myPath = [[UIBezierPath alloc] init];
myPath.lineWidth = 5;
UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
[myPath moveToPoint:[mytouch locationInView:self]];
[pathArray addObject:myPath];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
[myPath addLineToPoint:[mytouch locationInView:self]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
I found one demo code for that..
Please check this if it will be helpful to you..
Smooth Line link

Sliding a CCMenu

I have a Menu ("myMenu") containing CCMenuItemImages. I would like this menu to detect finger swipes and slide accordingly.
My problem is that the CCMenuItemImages seem to absorb the touch event. The swipe works fine when the user touches the menu outside of the CCMenuItemImages, but not when the touch happens on these.
I have tried to put my menu items in a layer to detect touches (refering to answer Scrollable menu using MenuItem's), but this doesn't seem to work either. Any idea why ?
+(id) scene
{
CCScene *scene = [CCScene node];
ModeMenuScene *layer = [ModeMenuScene node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init] )) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *background = [CCSprite spriteWithFile:#"bg.png"];
background.position=ccp(winSize.width/2,winSize.height/2);
[self addChild:background];
mode1 = [CCMenuItemImage itemFromNormalImage:#"Mode1.png" selectedImage: #"Mode1.png" target:self selector:#selector(goToMode1:)];
mode1label = [CCLabelTTF labelWithString:[NSString stringWithFormat:#"Level 1 %d", n] dimensions:CGSizeMake(220,53) alignment:UITextAlignmentCenter fontName:#"Arial" fontSize:20.0];
mode1label.color = ccc3(167,0,0);
mode1label.position=ccp(55,-30);
[mode1 addChild:mode1label];
// here same kind of code to define mode2,mode3,mode4 (taken out to reduce size of code)
myMenu=[CCMenu menuWithItems:mode1,mode2,mode3,mode4,nil];
[myMenu alignItemsHorizontallyWithPadding:25];
myMenu.position=ccp(winSize.width/2+40,180);
menuLayer = [CCLayer node];
[menuLayer addChild:myMenu];
[self addChild:menuLayer];
[self enableTouch];
}
return self;
}
-(void) disableTouch{
self.isTouchEnabled=NO;
menuLayer.isTouchEnabled=NO;
}
-(void) enableTouch{
self.isTouchEnabled=YES;
menuLayer.isTouchEnabled=YES;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
if(location.y>100 && location.y<260) {
draggingMenu=1;
x_initial = location.x;
}
else draggingMenu=0;
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
if(draggingMenu==1) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
int x = myMenu.position.x+location.x-x_initial;
x = MAX(0,x);
x = MIN(x,winSize.width/2+40);
myMenu.position=ccp(x,180);
x_initial=location.x;
}
}
- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
draggingMenu=0;
}
- (void)dealloc {
[super dealloc];
}
#end
Solved it by adding :
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:NO];
}
the problem was that CCMenuItemImage swallows the touches and has a high priority set at -128. Thus the need to set priority at INT_MIN+1

Scrolling content hidden by keyboard. xcode 4.3.2

Using xcode 4.3.2. I hope this is the last time I have to beg for help.
I have a screen with 4 text fields. The bottom 2 are hidden by the keyboard when editing. I want to be able to scroll down so these 2 hidden fields can be edited. I have pasted my .m and .h files. something is not quite right.
Viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollview;
#property (strong, nonatomic) IBOutlet UITextField *TextField1;
#property (strong, nonatomic) IBOutlet UITextField *TextField2;
#property (strong, nonatomic) IBOutlet UITextField *TextField3;
#property (strong, nonatomic) IBOutlet UITextField *TextField4;
#property (strong, nonatomic) IBOutlet UITextField *activefield;
- (IBAction)HideKeyBoard:(id)sender;
viewcontroler.m
#interface ViewController ()
#end
#implementation ViewController
#synthesize scrollview;
#synthesize TextField1;
#synthesize TextField2;
#synthesize TextField3;
#synthesize TextField4;
#synthesize activefield;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activefield = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activefield = nil;
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]
CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollview.contentInset = contentInsets;
scrollview.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activefield.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0,
activefield.frame.origin.y-kbSize.height);
[scrollview setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollview.contentInset = contentInsets;
scrollview.scrollIndicatorInsets = contentInsets;
}
- (void)viewDidUnload
{
[self setScrollview:nil];
[self setActivefield:nil];
[self setTextField1:nil];
[self setTextField2:nil];
[self setTextField3:nil];
[self setTextField4:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)HideKeyBoard:(id)sender {
}
#end
Sliding UITextFields around to avoid the keyboard
I have faced this problem too. The link above really helped me. I hope that it will help you too
In the .h file you have to create a property like this
#property CGFloat animatedDistance;
then in the .m file you have to add this:
#synthesize animatedDistance;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
then you have to add this method:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
That's it. Good luck : )

Resources