cocoa detect object in mouse clicked - objective-c

I created a custom object (NSImageView) to display images and I use addSubview: self.view to add
I want to determine if the mouse click in it.
How do I do?
Thank you.
NSImage *Image = [NSImage imageNamed:#"oneimage.jpg"];
NSImageView *ImageView = [[NSImageView alloc] init];
[ImageView setImage:Image];
[self.window addSubview:ImageView];
[ImageView_IconToAdd setAcceptsTouchEvents:YES];
[ImageView_IconToAdd setWantsRestingTouches:YES];
-(void)mouseDragged:(NSEvent *)theEvent {
id clickedObject = [self hitTest:[theEvent locationInWindow]];
if ([clickedObject isKindOfClass:[NSImageView class]]) {
NSLog(#"Clicked an ImageView");
} else if ([clickedObject isKindOfClass:[NSView class]]) {
NSLog(#"Clicked a WebView");
}
}
But error:
No visible #interface for 'WindowImage' declares the selector 'hitTest:'
And can't get Object!

Related

Using ELCImagePickerController to select images

I am trying to use the ELCImagePickerController , and it works but i have a few things that i would like to change .
i want it to show me a matrix of images from the main album right away, without picking albums and stuff. just show a matrix, with a done button . now it lets you pick an album first .
i cant understand where exactly the picked images where saved ?
how can i change the buttons cancel and done graphics ?
this is how i add it (to cocos2d scene )
albumController = [[ELCAlbumPickerController alloc] initWithNibName:#"ELCAlbumPickerController" bundle:[NSBundle mainBundle]];
elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
[albumController setParent:elcPicker];
[elcPicker setDelegate:self];
[[[CCDirector sharedDirector] view] addSubview:elcPicker.view];
this is the done button :
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
//[self removeFromParentAndCleanup:YES];
for (UIView *v in [scrollview subviews])
{
[v removeFromSuperview];
}
CGRect workingFrame = scrollview.frame;
workingFrame.origin.x = 0;
for(NSDictionary *dict in info)
{
UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[scrollview addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
[scrollview setPagingEnabled:YES];
[scrollview setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}

UIActionSheet backGroundColor on iPad

I'm struggeling with an issuse for quite a while now. Since I don't really find a solution, I hope somebody here will be able to help me.
I have a UIActionSheet that I want to have a different background color.
With
[[myAlert layer] setBackgroundColor:[UIColor redColor].CGColor];
I am able to change most of the alert's color.
This is how it looks like:
This is how I initialize the UIActionSheet:
UIActionSheet *styleAlert = [[UIActionSheet alloc] initWithTitle:AMLocalizedString(#"SELECT_PICTURE_TITLE", #"Überschrift des Actionsheets")
delegate:self
cancelButtonTitle:AMLocalizedString(#"CANCEL_BUTTON_TITLE", #"Abbrechen butten")
destructiveButtonTitle:nil
otherButtonTitles:AMLocalizedString(#"TAKE_PICTURE_BUTTON_TITLE", #"Bild aufnehmen button"),
AMLocalizedString(#"CAMERA_ROLL_BUTTON_TITLE", #"Aus Bibliothek auswählen"), nil];
// use the same style as the nav bar
[styleAlert showInView:self.parentViewController.tabBarController.view];
//[styleAlert showInView:[self.navigationController view] ];
[styleAlert release];
and
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
actionSheet.layer.backgroundColor = GLOBAL_TINT_COLOR.CGColor;
}
I could not figure out how to set the border with the same color. Any ideas?
Thanks in advance!
You can't redraw the border in a different color, so just remove the border and add your own:
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet {
UIView *contentView = actionSheet.superview;
UIView *popoverView = contentView.superview;
UIView *chromeView;
for (UIView *v in [popoverView subviews]) {
if (v.subviews.count == 3) {
chromeView = v;
break;
}
}
for (UIView *backgroundComponentView in [chromeView subviews]) {
backgroundComponentView.hidden = YES;
CGRect componentFrame = backgroundComponentView.frame; // add your view with this frame
}
}
Note that this won't work in *will*PresentActionSheet since actionSheet doesn't have a superview set at that point.
This works on iPhone to theme it, It will probably work on iPad also.
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed#""]];
imageView.frame = CGRectMake(0, 0, actionSheet.frame.size.width, actionSheet.frame.size.height);
[actionSheet insertSubview:imageView atIndex:0];
NSArray *subviews = [actionSheet subviews];
for (UIView *v in subviews) {
if ([v isKindOfClass:[UIButton class]]) {
//Theme the buttons on the action sheet
UIButton *b = (UIButton *)v;
}
else if ([v isKindOfClass:[UILabel class]]) {
UILabel *l = (UILabel *)v;
//Theme the label at the top of the action sheet
}
}
}

Does it make sense to add ATMHud to tabBarController.view

When i try to add ATMHud to uitableviewcontroller subview it does work but it doesn't disable the scrolling and if the tableview is not on top i can't see the hud view. What i did was added to teh tabBarController.view that works but i want find out if this is a good idea or later on i might have issues with it.
Another question is tabBarController.view frame is that the whole screen or just the bottom part. How come atmhud shows in the middle of the screen?
Thanks in advance!
Yan
============
Found a blog post that shows how to reset self.view and add tableview separately in uitableviewcontroller
UITableViewController and fixed sub views
- (void)viewDidLoad {
[super viewDidLoad];
if (!tableView &&
[self.view isKindOfClass:[UITableView class]]) {
tableView = (UITableView *)self.view;
}
self.view = [[[UIView alloc] initWithFrame:
[UIScreen mainScreen].applicationFrame] autorelease];
self.tableView.frame = self.view.bounds;
self.tableView.contentInset = UIEdgeInsetsMake(44.0, 0.0, 0.0, 0.0);
[self.view addSubview:self.tableView];
UIView *fixedBar = [[UIView alloc] initWithFrame:
CGRectMake(0.0, 0.0, self.view.bounds.size.width, 44.0)];
fixedBar.backgroundColor = [UIColor colorWithRed:
0.0 green:1.0 blue:0.0 alpha:0.7];
[self.view addSubview:fixedBar];
[fixedBar release];
}
After this when add hud to self.view you will be able to disable the tableview on the bottom.
Let me know if this a good way to setup the tableview
The problem with using the tab bar is that the hud is now modal, and the user cannot change the tab.
It sounds like the tableview is not your primary viewm, as it can get "covered up". If its not the primary view, then add the ATMHud to self.view. If the tableView is the same as self.view, then add a new transparent view to it, then add the HUD to that view.
The tabBarController.view is the view that hosts the tabbed views - if you want to see its size (or frame) log it using NSStringFromCGRect(self.tabBarController.frame);
EDIT: I just did a test, the ATMHud DOES block the UI. All I can think of is that you have not inserted it where you need to (at the top of current view's subviews.) I have a demo project where I do this:
hud = [[ATMHud alloc] initWithDelegate:self];
[self.view addSubview:hud.view];
[hud setCaption:#"Howdie"];
[hud setActivity:YES];
[hud show];
[hud hideAfter:5];
A button under the hud is not active - in fact nothing in the view is active (probably the Nav Bar would be live though)
If you want an ARCified and field tested version, you can grab it here
EDIT2: The solution to your problem is below. Note that ATMHud blocks clicks from getting to the table, and the code below stops the scrolling:
- (void)hudWillAppear:(ATMHud *)_hud
{
self.tableView.scrollEnabled = NO;
}
- (void)hudDidDisappear:(ATMHud *)_hud
{
self.tableView.scrollEnabled = YES;
}
Dump the views:
#import <QuartzCore/QuartzCore.h>
#import "UIView+Utilities.h"
#interface UIView (Utilities_Private)
+ (void)appendView:(UIView *)v toStr:(NSMutableString *)str;
#end
#implementation UIView (Utilities_Private)
+ (void)appendView:(UIView *)a toStr:(NSMutableString *)str
{
[str appendFormat:#" %#: frame=%# bounds=%# layerFrame=%# tag=%d userInteraction=%d alpha=%f hidden=%d\n",
NSStringFromClass([a class]),
NSStringFromCGRect(a.frame),
NSStringFromCGRect(a.bounds),
NSStringFromCGRect(a.layer.frame),
a.tag,
a.userInteractionEnabled,
a.alpha,
a.isHidden
];
}
#end
#implementation UIView (Utilities)
+ (void)dumpSuperviews:(UIView *)v msg:(NSString *)msg
{
NSMutableString *str = [NSMutableString stringWithCapacity:256];
while(v) {
[self appendView:v toStr:str];
v = v.superview;
}
[str appendString:#"\n"];
NSLog(#"%#:\n%#", msg, str);
}
+ (void)dumpSubviews:(UIView *)v msg:(NSString *)msg
{
NSMutableString *str = [NSMutableString stringWithCapacity:256];
if(v) [self appendView:v toStr:str];
for(UIView *a in v.subviews) {
[self appendView:a toStr:str];
}
[str appendString:#"\n"];
NSLog(#"%#:\n%#", msg, str);
}
#end

UIImagePickerController how to hide the flip camera button?

is there a way to hide the flip camera button inside the UIImagePickerController?
thanks for reading
!^_^!
I ended up using a custom subclass of UIImagePickerController to fix this (and other) issues:
#import "SMImagePickerController.h"
#implementation SMImagePickerController
void hideFlipButtonInSubviews(UIView *view) {
if ([[[view class] description] isEqualToString:#"CAMFlipButton"]) {
[view setHidden:YES];
} else {
for (UIView *subview in [view subviews]) {
hideFlipButtonInSubviews(subview);
}
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
hideFlipButtonInSubviews(self.view);
}
#end
You should be able to create an empty button inside an overlayview that you float on top of the flip camera button. I hacked the code below to test and it seemed to work. Give it a try.
UIView *cameraOverlayView = [[UIView alloc] initWithFrame:CGRectMake(screenSize.width - 100.0f, 5.0f, 100.0f, 35.0f)];
[cameraOverlayView setBackgroundColor:[UIColor blackColor]];
UIButton *emptyBlackButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 35.0f)];
[emptyBlackButton setBackgroundColor:[UIColor blackColor]];
[emptyBlackButton setEnabled:YES];
[cameraOverlayView addSubview:emptyBlackButton];
cameraUI.allowsEditing = YES;
cameraUI.showsCameraControls = YES;
cameraUI.delegate = self;
cameraUI.cameraOverlayView = cameraOverlayView;

Add UIImageView as a subview to UIKeyboard - userInteractionEnabled?

I have a little problem with adding a subview to my UIKeyboard. I can successfully locate my keyboard and add an uiimageview to it, but unfortunately the button under the image view still can be touched. I just wanted to make it a color overlay, which would hide the unneeded keys and also make them untouchable :)
Here is my code, what am I doing wrong? The UIImageView displays correctly, but the buttons still can be tapped... :(
- (UIView *)findKeyboard {
// Locate non-UIWindow.
UIWindow *keyboardWindow = nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) {
if (![[testWindow class] isEqual:[UIWindow class]]) {
keyboardWindow = testWindow;
break;
}
}
// Locate UIKeyboard.
UIView *foundKeyboard = nil;
for (UIView __strong *possibleKeyboard in [keyboardWindow subviews]) {
// iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
if ([[possibleKeyboard description] hasPrefix:#"<UIPeripheralHostView"]) {
possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
}
if ([[possibleKeyboard description] hasPrefix:#"<UIKeyboard"]) {
foundKeyboard = possibleKeyboard;
break;
}
}
return foundKeyboard;
}
- (void)keyboardWillShow:(NSNotification *)note {
UIImageView *overlayView;
UIView *keyboardView = [self findKeyboard];
UIImage *img = [UIImage imageNamed:#"keyboardOverlay.png"];
overlayView = [[UIImageView alloc] initWithImage:img];
[overlayView setUserInteractionEnabled:NO];
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, overlayView.frame.size.width, overlayView.frame.size.height)];
newView.userInteractionEnabled = NO;
newView.multipleTouchEnabled = NO;
[newView addSubview:overlayView];
[keyboardView insertSubview:newView atIndex:40];
}
User interaction being disabled means that touches are passed straight through. Enable it, but don't have any gesture recognisers or actions, and it will swallow all touches.