Posterous theme by Cory Watilo

Filed under: programming

Intercepting UIButton Touch Events

I was recently building an iPhone app that required a UIButton be held down and then released after a certain period of time had elapsed. The problem I encountered was that testing for touchesBegan didn't work for buttons. I'm not talking about UIControlEvents. I didn't need to know that button had been tapped or it's current state. I needed to know when the button was being pressed and when it was released. The solution was to simply create a UIButton subclass and pass it's touches up the responder chain. MyButton.h - [code lang="cpp"] @interface MyButton : UIButton { } - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; @end [/code] MyButton.m - [code lang="cpp"] #import "BrakeButton.h" @implementation BrakeButton - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.nextResponder touchesBegan:touches withEvent:event]; // This is where the touch is passed on } - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self.nextResponder touchesEnded:touches withEvent:event]; // This is where the touch is passed on } @end [/code] Then in your view controller be sure to import your new class: [code lang="cpp"] #import "MyButton.h" [/code] Then you simply implement the touchesBegan and touchesEnded methods. [code lang="cpp"] -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Button Touches began"); } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Button Touches ended"); } [/code] So what is happening here? The main point to understand is that UIButtons are in actuality just UIViews. So they inherit from UIViews giving them the ability to access UIView-like functionality. (touchesBegan, touchesEnded, etc...) I've seen many folks banging their heads against the wall trying to figure out how to capture / intercept button touches so I apparently it's a pretty common problem. Let me know if you find this helpful.

Getting Ruby-on-Rails Up On Ubuntu / Apache

GCC: sudo apt-get install build-essential Ruby & Friends: sudo apt-get install ruby1.8-dev ruby1.8 ri1.8 rdoc1.8 irb1.8 libreadline-ruby1.8 libruby1.8 *** Create Symbolic Links *** sudo ln -s /usr/bin/ruby1.8 /usr/local/bin/rubysudo ln -s /usr/bin/ri1.8 /usr/local/bin/risudo ln -s /usr/bin/rdoc1.8 /usr/local/bin/rdocsudo ln -s /usr/bin/irb1.8 /usr/local/bin/irbZlib:wget http://www.zlib.net/zlib-1.2.3.tar.gztar -xvf zlib-1.2.3.tar.gz./configure make sudo make install Install Open SSL: sudo apt-get install openssl libssl-dev Apache 2.2: wget http://apache.rmplc.co.uk/httpd/httpd-2.2.4.tar.gz tar -xvf httpd-2.2.4.tar.gz ./configure --prefix=/usr/local/apache --enable-proxy --enable-proxy-http --enable-proxy-balancer --enable-dav --enable-rewrite --enable-so --enable-http --enable-ssl --enable-expires --enable-headers --enable-mods=deflate_module --with-php --with-mysql --with-susexec --disable-info --without-berkeley-db --enable-dav=shared --enable-dav-lock=shared --with-included-apr make sudo make install MySQL & Postfix Postfix: sudo apt-get install postfix note: select "Internet Site" option when prompted MySQL: sudo apt-get install mysql-server mysql-common mysql-client libmysqlclient15-dev libmysqlclient15off sudo apt-get install libmysql-ruby1.8 /* MySQL Ruby bindings */ sudo apt-get install libmysqlclient15-dev libmysql-ruby1.8sudo gem install mysql note: Select the most recent non-win32 version FastCGI: sudo apt-get install libfcgi libfcgi-dev libfcgi-ruby1.8 sudo gem install fcgi Lighttpd: apt-get install lighttpd RubyGems: wget http://rubyforge.iasi.roedu.net/files/rubygems/rubygems-0.9.0.tgz tar xvzf rubygems* cd rubygems* sudo ruby setup.rb Rails: sudo gem install rails --include-dependencies Mongrel, Subversion and Capistrano Mongrel: sudo gem install daemons gem_plugin mongrel mongrel_cluster --include-dependencies Subversion: sudo apt-get install libxml2-dev wget http://subversion.tigris.org/downloads/subversion-1.4.5.tar.gz tar xfz subversion-1.4.5.tar.gz cd subversion-1.4.5 ./configure --with-apr=/usr/local/apache/bin/apr-1-config --with-apr-util=/usr/local/apache/bin/apu-1-config --with-apxs=/usr/local/apache/bin/apxs --without-berkeley-db --with-ssl Capistrano sudo gem install deprec --include-dependencies Redcloth sudo gem install redcloth